Configurable HashId Trait for Laravel Eloquent models

2.0.0 2023-08-01 10:53 UTC

This package is auto-updated.

Last update: 2024-06-13 17:13:49 UTC


Latest Version

This package provides a trait to easily encode and decode hashids in your Eloquent models. Every model gets an attribute and a scope for hashid queries.


You can install this package with Composer.

Require the package via Composer:

$ composer require intervention/eloquent-hashid

Laravel will automatically discover the packages service provider class.


After installation you're able to publish the configuration file to your Laravel application with the following command.

$ php artisan vendor:publish --provider="Intervention\EloquentHashid\Laravel\EloquentHashidServiceProvider"

You will find a new config file in config/hashid.php, which you can customize. I strongly suggest to change at least the salt_prefix option to your own value.

Now you can include the trait Intervention\EloquentHashid\HasHashid in your Eloquent Model to add the hashid features.

use Intervention\EloquentHashid\HasHashid;

class Item extends Model
    use HasHashid;


Code Example

Every Eloquent Model gets a new hashid attribute, which is created based on the models classname, the key and the salt prefix. You're also able to query models with the now added hashid() scope.

Access hashid attribute

$item = App\Models\Item::find(123);

// access hashid attribute
$hashid = $item->hashid

Query models

// query model with scope
$item = App\Models\Item::hashid('Ma93ka')->firstOrFail();

// scope can also select multiple items by array
$item = App\Models\Item::hashid(['Ma93ka', 'Op92ae'])->get();

// scope query in one call
$item = App\Models\Item::findByHashid('Ma93ka');

// scope query in one call, throw exception if no model was found
$item = App\Models\Item::findByHashidOrFail('Ma93ka');

Route Model Binding

Eloquent models can be resolved by their hashid in routes by defining a custom key.

use App\Models\Item;

Route::get('/items/{item:hashid}', function (Item $item) {
    return $item;


This library is developed and maintained by Oliver Vogel


Intervention Eloquent HashID is licensed under the MIT License.