PHPackages                             mvaliolahi/smart-hash - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mvaliolahi/smart-hash

ActiveLibrary

mvaliolahi/smart-hash
=====================

Laravel Smart Hash Model id and request.

v0.0.17(2y ago)359MITPHP

Since Jul 13Pushed 2y ago1 watchersCompare

[ Source](https://github.com/mvaliolahi/smart-hash)[ Packagist](https://packagist.org/packages/mvaliolahi/smart-hash)[ RSS](/packages/mvaliolahi-smart-hash/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (4)Versions (18)Used By (0)

Laravel Eloquent Models Smart-HashIDs
=====================================

[](#laravel-eloquent-models-smart-hashids)

### What is the hashids?

[](#what-is-the-hashids)

> Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers. It converts numbers like 347 into strings like “yr8”, or array of numbers like \[27, 986\] into “3kTMd”.

For more information visit [hashids.org](https://hashids.org/).

Why we should use this library?
===============================

[](#why-we-should-use-this-library)

Your client deals with hash id, but each time your app received `id` or `ids` in the request SmartHash will decode it for you.

- Easy to use!
- Hidden your database ids from client.
- Automatic encode model id for front-end and decode for back-end (Decode all `id` or `ids` requests from parameters or the body)
- Does not need to methods like `findByHashid`

Installation
============

[](#installation)

```
$ composer require mvaliolahi/smart-hash

$ php artisan vendor:publish
```

How to use
----------

[](#how-to-use)

1. inside your Model use `SmartHash` trait.

```
class Category extends Model
{
    use SmartHash;
}

$category->hashId()
```

After use SmartHash Trait, Category::first()-&gt;id will return `jR` not `1`, You can access to original id using the $category-&gt;id() method.

2. Inside you boot method in AppServiceProvider define your models because of `route model binding`:

```
$this->app->singleton('smart-hash', function() {
    return [
        'category' => Category::class,
        'user'     => User::class,
    ];
});
```

Add a new parameter to decoder lookup-table
-------------------------------------------

[](#add-a-new-parameter-to-decoder-lookup-table)

edit `config/smart-hash.php` file, next step is obvious! for example, if we want to auto decode parameters like `id`, `category_id` and array parameter like `ids`, config is like below.

```
