On-the-fly hashids for Laravel Eloquent models.

v2.0.0 2020-03-15 15:15 UTC

This package is auto-updated.

Last update: 2020-07-30 13:06:03 UTC


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)


$ 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;

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

// 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 the integer id 

// Getting the name of the hashid connection

Route Binding

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