Using hashids instead of integer ids in urls and list items can be more appealing and clever. For more information visit hashids.org.
This adds hashids to Laravel Eloquent models by encoding/decoding them on the fly rather than persisting them in the database. So no need for another database column and also higher performance by using primary keys in queries.
Features include:
- Generating hashids for models
- Resloving hashids to models
- Ability to customize hashid settings for each model
- Route binding with hashids (optional)
$ composer require mtvs/eloquent-hashids
Base features are provided by using HasHashid
trait then route binding with
hashids can be added by using HashidRouting
.
use Illuminate\Database\Eloquent\Model;
use Mtvs\EloquentHashids\HasHashid;
use Mtvs\EloquentHashids\HashidRouting;
Class Item extends Model
{
use HasHashid, HashidRouting;
}
It's possible to customize hashids settings for each model by overwriting
getHashidsConnection()
. It must return the name of a connection of
vinkla/hashids
that provides
the desired settings.
// Generating the model hashid based on its key
$item->hashid();
// Finding a model based on the provided hashid or
// returning null on failure
Item::findByHashid($hashid);
// Finding a model based on the provided hashid or
// throwing a ModelNotFoundException on failure
Item::findByHashidOrFail($hashid);
// Decoding a hashid to the integer id
$item->hashidToId($hashid);
// Getting the name of the hashid connection
$item->getHashidsConnection();
When HashidRouting
trait is used, base getRouteKey()
and resolveRouteBinding()
are overwritten to use hashids as route keys.