PHPackages                             mtvs/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. mtvs/eloquent-hashids

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

mtvs/eloquent-hashids
=====================

On-the-fly hashids for Laravel Eloquent models.

v3.6.0(1y ago)291431.0k—3.5%25[4 PRs](https://github.com/mtvs/eloquent-hashids/pulls)MITPHPCI failing

Since Aug 7Pushed 1y ago6 watchersCompare

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

READMEChangelogDependencies (5)Versions (17)Used By (0)

> Using hashids instead of integer ids in urls and list items can be more appealing and clever. For more information visit [hashids.org](https://hashids.org/).

Eloquent-Hashids [![Build Status](https://github.com/mtvs/eloquent-hashids/actions/workflows/build.yml/badge.svg)](https://github.com/mtvs/eloquent-hashids/actions/workflows/build.yml/badge.svg)
==================================================================================================================================================================================================

[](#eloquent-hashids-)

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)

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

[](#installation)

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
```

Setup
-----

[](#setup)

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

[](#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`](https://github.com/vinkla/laravel-hashids) that provides the desired settings.

Usage
-----

[](#usage)

### Basics

[](#basics)

```
// Generating the model hashid based on its key
$item->hashid();

// Equivalent to the above but with the attribute style
$item->hashid;

// Finding a model based on the provided hashid or
// returning null on failure
Item::findByHashid($hashid);

// Finding a model based on the provided hashid or
// throwing a ModelNotFoundException on failure
Item::findByHashidOrFail($hashid);

// Decoding a hashid to its equivalent id
$item->hashidToId($hashid);

// Encoding an id to its equivalent hashid
$item->idToHashid($id);

// Getting the name of the hashid connection
$item->getHashidsConnection();
```

### Add the hashid to the serialized model

[](#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();`

### Implicit Route Bindings

[](#implicit-route-bindings)

If you want to resolve implicit route bindings for the model using its hahsid value you can use `HashidRouting` in the model.

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

class Item extends Model
{
    use HasHashid, HashidRouting;
}
```

It overwrites `getRouteKeyName()`, `getRouteKey()` and `resolveRouteBindingQuery()`to use the hashids as the route keys.

It supports the Laravel's feature for customizing the key for specific routes.

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

#### Customizing The Default Route Key Name

[](#customizing-the-default-route-key-name)

If you want to by default resolve the implicit route bindings using another field you can overwrite `getRouteKeyName()` to return the field's name to the resolving process and `getRouteKey()` to return its value in your links.

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

class Item extends Model
{
    use HasHashid, HashidRouting;

    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function getRouteKey()
    {
        return $this->slug;
    }
}
```

You'll still be able to specify the hashid for specific routes.

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

#### Supporting The Other Laravel's Implicit Route Binding Features

[](#supporting-the-other-laravels-implicit-route-binding-features)

When using `HashidRouting` you'll still be able to use softdeletable and child route bindings.

```
Route::get('/items/{item}', function (Item $item) {
    return $item;
})->withTrashed();

Route::get('/user/{user}/items/{item}', function (User $user, Item $item) {
    return $item;
})->scopeBindings();
```

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance46

Moderate activity, may be stable

Popularity55

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 88.6% 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 ~158 days

Recently: every ~285 days

Total

14

Last Release

415d ago

Major Versions

v1.1.1 → v2.0.02020-03-15

v2.0.0 → 3.x-dev2020-09-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/f02aa2b87486bfd83077bcc587ccc5891ddf49d1dfe834e2ac749d81537b2cce?d=identicon)[mtvs](/maintainers/mtvs)

---

Top Contributors

[![mtvs](https://avatars.githubusercontent.com/u/8286154?v=4)](https://github.com/mtvs "mtvs (62 commits)")[![npostman](https://avatars.githubusercontent.com/u/5596683?v=4)](https://github.com/npostman "npostman (3 commits)")[![crynobone](https://avatars.githubusercontent.com/u/172966?v=4)](https://github.com/crynobone "crynobone (2 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")[![leewillis77](https://avatars.githubusercontent.com/u/1097338?v=4)](https://github.com/leewillis77 "leewillis77 (1 commits)")[![lucastbucks](https://avatars.githubusercontent.com/u/148023244?v=4)](https://github.com/lucastbucks "lucastbucks (1 commits)")

---

Tags

eloquenteloquent-modelshashidslaravelphp

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[staudenmeir/eloquent-json-relations

Laravel Eloquent relationships with JSON keys

1.1k5.8M24](/packages/staudenmeir-eloquent-json-relations)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.1M11](/packages/bavix-laravel-wallet)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[gearbox-solutions/eloquent-filemaker

A package for getting FileMaker records as Eloquent models in Laravel

6454.8k2](/packages/gearbox-solutions-eloquent-filemaker)[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)

PHPackages © 2026

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