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.
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
orids
requests from parameters or the body) - Does not need to methods like
findByHashid
$ composer require mvaliolahi/smart-hash
$ php artisan vendor:publish
- inside your Model use
SmartHash
trait.
class Category extends Model
{
use SmartHash;
}
$category->hashId()
After use SmartHash Trait, Category::first()->id will return jR
not 1
, You can access to original id using the $category->id() method.
- 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,
];
});
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.
<?php
return [
'single_parameters' => [
'id',
'category_id'
],
'array_parameters' => [
'ids',
]
];
$post = Post::findByHash('vm');
$post = Post::findOrFailByHash('vm');