PHPackages                             antoninmasek/laravel-model-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. antoninmasek/laravel-model-hashids

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

antoninmasek/laravel-model-hashids
==================================

Easily use Hashids with Laravel models.

1.1.0(11mo ago)27.3k1MITPHPPHP ^8.1CI passing

Since Jun 12Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/antoninmasek/laravel-model-hashids)[ Packagist](https://packagist.org/packages/antoninmasek/laravel-model-hashids)[ Docs](https://github.com/antoninmasek/laravel-model-hashids)[ RSS](/packages/antoninmasek-laravel-model-hashids/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (7)Versions (10)Used By (0)

Easily use Hashids with Laravel models.
=======================================

[](#easily-use-hashids-with-laravel-models)

[![Packagist Version](https://camo.githubusercontent.com/12c6ffb7ed58a823246aed07e3163228d5cdc3afbd31baa0a54a4e45f8054e84/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e746f6e696e6d6173656b2f6c61726176656c2d6d6f64656c2d686173686964733f7374796c653d666c61742d737175617265266c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b61676573253246616e746f6e696e6d6173656b2532466c61726176656c2d6d6f64656c2d68617368696473)](https://camo.githubusercontent.com/12c6ffb7ed58a823246aed07e3163228d5cdc3afbd31baa0a54a4e45f8054e84/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e746f6e696e6d6173656b2f6c61726176656c2d6d6f64656c2d686173686964733f7374796c653d666c61742d737175617265266c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b61676573253246616e746f6e696e6d6173656b2532466c61726176656c2d6d6f64656c2d68617368696473)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/388785bda15a0c98afdedcbdde6fa2858a7cc379be4416fc1af538c814fa4828/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616e746f6e696e6d6173656b2f6c61726176656c2d6d6f64656c2d686173686964732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6162656c3d7465737473)](https://camo.githubusercontent.com/388785bda15a0c98afdedcbdde6fa2858a7cc379be4416fc1af538c814fa4828/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616e746f6e696e6d6173656b2f6c61726176656c2d6d6f64656c2d686173686964732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6162656c3d7465737473)[![Packagist Downloads](https://camo.githubusercontent.com/359b38a6110e454505061a3fec3d9d62b1c82d3fa1444080744c159fe563a5d0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e746f6e696e6d6173656b2f6c61726176656c2d6d6f64656c2d686173686964733f7374796c653d666c61742d737175617265266c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b61676573253246616e746f6e696e6d6173656b2532466c61726176656c2d6d6f64656c2d68617368696473)](https://camo.githubusercontent.com/359b38a6110e454505061a3fec3d9d62b1c82d3fa1444080744c159fe563a5d0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e746f6e696e6d6173656b2f6c61726176656c2d6d6f64656c2d686173686964733f7374796c653d666c61742d737175617265266c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b61676573253246616e746f6e696e6d6173656b2532466c61726176656c2d6d6f64656c2d68617368696473)

In some cases I really like to use [Hashids](https://hashids.org/php/) instead of uuids as my model keys. For me Hashids are less awkward to read and the resulting URL looks a bit nicer in my opinion. This package is inspired by [laravel-model-uuid](https://github.com/michaeldyrynda/laravel-model-uuid) by Michael Dyrynda and aims to make it a breeze to start using Hashids as your model keys.

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

[](#installation)

You can install the package via composer:

```
composer require antoninmasek/laravel-model-hashids
```

You can publish the config file with:

```
php artisan vendor:publish --tag="laravel-model-hashids-config"
```

This is the contents of the published config file:

```
return [
    /*
     * The following column will be filled with the generated hash id. If you decide to also bind
     * models to hash_id, then this column will be used as route key name.
     */
    'hash_id_column' => 'hash_id',

    /*
     * Define the column name, which will be used to generate the hash id. This column has to contain
     * a numeric value or an array of numbers and should be unique for each model to prevent
     * potential collisions.
     */
    'model_key' => 'id',
];
```

> This package uses [antoninmasek/laravel-hashids](https://github.com/antoninmasek/laravel-hashids) in the background. And if you wish to configure some aspects of the underlying hash id generation, then please take a look at a readme of the package.

Usage
-----

[](#usage)

To use this package you'll be most interested in the following two traits: `GeneratesHashId` and `BindsOnHashId`.

### Generating hash id

[](#generating-hash-id)

In order for your model to automatically get a hash id after it is created just use `GeneratesHashId` trait on your model:

```
use AntoninMasek\Hashids\Traits\GeneratesHashId;

class YourModel extend Model
{
    use GeneratesHashId;
}
```

### Binding on hash id

[](#binding-on-hash-id)

To also bind your model to hash id you also need to use `BindsOnHashId` trait:

```
use AntoninMasek\Hashids\Traits\GeneratesHashId;
use AntoninMasek\Hashids\Traits\BindsOnHashId;

class YourModel extend Model
{
    use GeneratesHashId;
    use BindsOnHashId;
}
```

Configuration
-------------

[](#configuration)

If you need to execute some logic in order to determine salt/alphabet/minlength you have a few callbacks at your disposal:

### Global

[](#global)

If you want to set these globally you may use the following callbacks. The callback is supplied with the model as a parameter.

```
use AntoninMasek\Hashids\ModelHashids;

ModelHashids::generateSaltUsing(function(Model $model) {
    // your logic
    return $salt;
});

ModelHashids::generateMinLengthUsing(function(Model $model) {
    // your logic
    return $minLength;
});

ModelHashids::generateAlphabetUsing(function(Model $model) {
    // your logic
    return $alphabet;
});
```

Local
-----

[](#local)

If you wish to have a specific logic just for a certain model you may define these methods on the desired model:

```
// Overwrite the column to fill with hash id for this specific model
public function hashIdColumn(): string;

// Overwrite the column to use for hash id generation for this specific model
public function hashIdKeyColumn(): string;

// Overwrite the logic to generate salt for this specific model
public function hashIdSalt(): string;

// Overwrite the logic to generate alphabet for this specific model
public function hashIdAlphabet(): string;

// Overwrite the logic to generate min length for this specific model
public function hashIdMinLength(): string;
```

### Precedence

[](#precedence)

This is the order in which the values are taken:

1. Model specific logic
2. Global logic
3. Config values
4. Hashids package

Regenerating hash id
--------------------

[](#regenerating-hash-id)

If you wish to regenerate a hash id for a particular model with current configuration you may do so as follows:

```
// This will save the new hash id directly to database
$model->regenerateHashId();

// This will just regenerate the hash id on the instance without persisting it.
// You will need to call the save() method to persist it.
$model->regenerateHashId(saveToDatabase: false);
```

Limitations
-----------

[](#limitations)

If your model key is auto-incrementing then, at least at the moment, there are 2 round-trips to the database. 1st to create the model and receive the ID and then 2nd to set the hash\_id based on the ID.

> **Note:** `Updating` eloquent event is not fired, when setting the `hash_id`. This is the default behaviour since version 0.6.0. It is still possible to change this via `save_quietly` config setting.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

[Antonín Mašek](https://github.com/antoninmasek)

License
-------

[](#license)

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

Special thanks
--------------

[](#special-thanks)

- To Michael Dyrynda for his [laravel-model-uuid](https://github.com/michaeldyrynda/laravel-model-uuid) package, by which this package is heavily inspired.
- To [Spatie](https://spatie.be/) for their amazing [skeleton](https://github.com/spatie/package-skeleton-laravel) which I used to scaffold this repository.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance52

Moderate activity, may be stable

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 96.9% 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

Every ~136 days

Recently: every ~262 days

Total

9

Last Release

336d ago

Major Versions

0.6.0 → 1.0.02024-04-01

PHP version history (3 changes)0.1.0PHP ^8.0|^8.1

0.5.0PHP ^8.1 || ^8.2

1.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/8faef5fac979c7f7fa3622af216db9734384499fa3890e62e9f6e97b421b204f?d=identicon)[antoninmasek](/maintainers/antoninmasek)

---

Top Contributors

[![antoninmasek](https://avatars.githubusercontent.com/u/31475951?v=4)](https://github.com/antoninmasek "antoninmasek (31 commits)")[![sweptsquash](https://avatars.githubusercontent.com/u/9886472?v=4)](https://github.com/sweptsquash "sweptsquash (1 commits)")

---

Tags

laravelantoninmaseklaravel-model-hashids

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/antoninmasek-laravel-model-hashids/health.svg)

```
[![Health](https://phpackages.com/badges/antoninmasek-laravel-model-hashids/health.svg)](https://phpackages.com/packages/antoninmasek-laravel-model-hashids)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)

PHPackages © 2026

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