Awesome Open Source
Awesome Open Source

Using hashids instead of integer ids in urls and list items can be more appealing and clever. For more information visit

Eloquent-Hashids Build Status

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)


Install the package with Composer:

$ composer require mtvs/eloquent-hashids

Also, publish the vendor config files to your application (necessary for the dependencies):

$ php artisan vendor:publish


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;

Custom Hashid Settings

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

// Equivalent to the above but with the attribute style

// Finding a model based on the provided hashid or
// returning null on failure

// Finding a model based on the provided hashid or
// throwing a ModelNotFoundException on failure

// Decoding a hashid to its equivalent id 

// Encoding an id to its equivalent hashid

// Getting the name of the hashid connection

Add the hashid to the serialized model

Set it as default:

use Illuminate\Database\Eloquent\Model;
use Mtvs\EloquentHashids\HasHashid;

class Item extends Model
    use HasHashid;
     * The accessors to append to the model's array form.
     * @var array
    protected $appends = ['hashid'];

or specify it specificly:

return $item->append('hashid')->toJson();

Route Binding

When HashidRouting trait is used, base getRouteKey() and resolveRouteBinding() are overwritten to use hashids as route keys.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Php (403,009
Laravel (13,043
Eloquent (527
Related Projects