PHPackages                             matt-daneshvar/eloquent-hashids - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. matt-daneshvar/eloquent-hashids

ActiveLibrary[Database &amp; ORM](/categories/database)

matt-daneshvar/eloquent-hashids
===============================

Automatically generate and persist Hashids for newly created Eloquent models.

v1.0.0(6y ago)171.0k2MITPHP

Since Jun 26Pushed 5y agoCompare

[ Source](https://github.com/matt-daneshvar/eloquent-hashids)[ Packagist](https://packagist.org/packages/matt-daneshvar/eloquent-hashids)[ RSS](/packages/matt-daneshvar-eloquent-hashids/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Eloquent Hashids for Laravel
============================

[](#eloquent-hashids-for-laravel)

[![Build Status](https://camo.githubusercontent.com/94ab20f627158c1c933d1e1618e70c1a07a749413bbf4f6ac7cd342a58808184/68747470733a2f2f7472617669732d63692e6f72672f6d6174742d64616e6573687661722f656c6f7175656e742d686173686964732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/matt-daneshvar/eloquent-hashids)[![GitHub](https://camo.githubusercontent.com/73188cbb4caaaaa0a898c22f0ac1bb296aac042901bed18f9a56ca07256173f5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6174742d64616e6573687661722f656c6f7175656e742d686173686964732e737667)](https://camo.githubusercontent.com/73188cbb4caaaaa0a898c22f0ac1bb296aac042901bed18f9a56ca07256173f5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6174742d64616e6573687661722f656c6f7175656e742d686173686964732e737667)

Automatically persist Hashids on your newly created Eloquent models using Ivan Akimov's [Hashids library](https://github.com/ivanakimov/hashids.php).

This can be useful when you need to generate unique alphanumeric (or any other character) combinations to represent your models.

Installation
------------

[](#installation)

Require the package using composer.

```
composer require matt-daneshvar/eloquent-hashids
```

Usage
-----

[](#usage)

Add a *nullable* `hashid` column to your database table in your migrations.

```
$table->string('hashid')->nullable();
```

Use the `Hashid` trait to automatically generate and persist Hashids for your new models. Optionally use `HashidRouting` to set your model to use the `hashid` column for Laravel's [Route Model Binding](https://laravel.com/docs/routing#route-model-binding).

```
use Illuminate\Database\Eloquent\Model;
use MattDaneshvar\EloquentHashids\Hashid;
use MattDaneshvar\EloquentHashids\HashidRouting;

class Receipt extends Model
{
    use Hashid, HashidRouting;
}
```

### Customizing Hashid generation

[](#customizing-hashid-generation)

While the package attempts to use sensible defaults to minimize configuration out of the box, you're free to adjust the Hashid generation behaviour using static properties on your model definition.

```
class Receipt extends Model
{
    use Hashid;

    /**
     * The column used to store Hashid.
     *
     * @var array
     */
    protected static $hashidColumn = 'hashid';

    /**
     * The minimum length of the generated Hashids.
     *
     * @var array
     */
    protected static $hashidMinLength = 8;

    /**
     * The whitelist of characters used inside the generated Hashids.
     *
     * @var array
     */
    protected static $hashidChars = 'abcdefghijklmnopqrstuvwxyz1234567890';

    /**
     * The salt for generating Hashids.
     *
     * @var array
     */
    protected static $hashidSalt = 'your unique salt';

    /**
     * The attribute encoded to generate the Hashid.
     *
     * @var array
     */
    protected static $hashidKey = 'id';
}
```

### Changing the Hashid column

[](#changing-the-hashid-column)

To customize the hashid column, set your own custom `$hashidColumn` value on your model.

```
class Receipt extends Model
{
    use Hashid;

    protected static $hashidColumn = 'uid';
}
```

### Changing the salt

[](#changing-the-salt)

Each model's table name is by default used as the salt for generating Hashids. With that, models of separate classes that share the same IDs (e.g. a `Task` model with ID of 1 and a `Receipt` model also with ID of 1) would each have different Hashids. You may change this behaviour and override the salt by specifying the `$hashidSlat` on your model.

```
class Receipt extends Model
{
    use Hashid;

    protected static $hashidSalt = 'salt and pepper';
}
```

### Creating your own Hashids instance

[](#creating-your-own-hashids-instance)

To fully customize the behaviour of the underlying Hashids library, you may also define your own `Hashids` instance in your model's boot method. Note that your Hashids instance would take precedence over all other customizations, and therefore all the rest of the static Hashid properties on your model (i.e. `$hashidMinLength`, `$hashidChars`, etc.) would be ignored once you specify your own `Hashids` instance.

```
class Receipt extends Model
{
    public static function boot()
    {
        parent::boot();

        static::$hashidsInstance = new Hashids('salt and pepper', 5);
    }
}
```

### Using the HashidRouting trait

[](#using-the-hashidrouting-trait)

A common use case of Hashids with Eloquent models is to use short URLs using the generated Hashids as identifiers.

For example you may wish to represent your app's receipts using their Hashid values:

```
https://example.com/receipts/2ov7j3o3

```

instead of their IDs:

```
https://example.com/receipts/4

```

For more convenience this package comes with a `HashidRouting` trait out of the box; once added to your model, this trait will change the model's route key name to its corresponding Hashid column, which would allow you to take advantage of Laravel's [Route Model Binding](https://laravel.com/docs/routing#route-model-binding)and use the Hashid URLs:

```
Route::get('api/receipts/{receipt}', function (App\Receipt $receipt) {
    return $receipt->total;
});
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2512d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4dd4c3943583b0db4572b8702f3ee8f85a3fa7ecf99401d5f1add6613821f031?d=identicon)[matt-daneshvar](/maintainers/matt-daneshvar)

---

Top Contributors

[![matt-daneshvar](https://avatars.githubusercontent.com/u/10030505?v=4)](https://github.com/matt-daneshvar "matt-daneshvar (21 commits)")

---

Tags

eloquenthashidslaravel

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/matt-daneshvar-eloquent-hashids/health.svg)

```
[![Health](https://phpackages.com/badges/matt-daneshvar-eloquent-hashids/health.svg)](https://phpackages.com/packages/matt-daneshvar-eloquent-hashids)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[kirkbushell/eloquence

A set of extensions adding additional functionality and consistency to Laravel's awesome Eloquent library.

573970.0k1](/packages/kirkbushell-eloquence)[stancl/virtualcolumn

Eloquent virtual column.

826.8M13](/packages/stancl-virtualcolumn)[veelasky/laravel-hashid

HashId Implementation on Laravel Eloquent ORM

46168.5k3](/packages/veelasky-laravel-hashid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
