PHPackages                             friendsofhyperf/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. friendsofhyperf/model-hashids

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

friendsofhyperf/model-hashids
=============================

The hashids for Hyperf models.

v3.1.75(5mo ago)21.7k1MITPHP

Since Mar 15Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/friendsofhyperf/model-hashids)[ Packagist](https://packagist.org/packages/friendsofhyperf/model-hashids)[ Fund](https://hdj.me/sponsors/)[ GitHub Sponsors](https://github.com/huangdijia)[ RSS](/packages/friendsofhyperf-model-hashids/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (39)Used By (0)

model-hashids
=============

[](#model-hashids)

> 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/).

[![Latest Stable Version](https://camo.githubusercontent.com/8178ea8f8dd7094047c3e2ac73b06a747b37bc40fa575f9ddf9aa70931dcaa1f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f667269656e64736f666879706572662f6d6f64656c2d68617368696473)](https://packagist.org/packages/friendsofhyperf/model-hashids)[![Total Downloads](https://camo.githubusercontent.com/46951275d82b922884c1e479932732e59c8af6aba093afa0d1918c71ba15833b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f667269656e64736f666879706572662f6d6f64656c2d68617368696473)](https://packagist.org/packages/friendsofhyperf/model-hashids)[![License](https://camo.githubusercontent.com/dd287405ca112aa03f48bcdfbe43127d5ed327bcd1b7ec48e6336749ccdb96fe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f667269656e64736f666879706572662f6d6f64656c2d68617368696473)](https://github.com/friendsofhyperf/model-hashids)

This adds hashids to Hyperf 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 friendsofhyperf/model-hashids
```

Also, publish the vendor config files to your application (necessary for the dependencies):

```
php bin/hyperf.php vendor:publish friendsofhyperf/model-hashids
```

Setup
-----

[](#setup)

Base features are provided by using `HasHashid` trait then route binding with hashids can be added by using `HashidRouting`.

```
use Hyperf\Database\Model\Model;
use FriendsOfHyperf\ModelHashids\Concerns\HasHashid;
use FriendsOfHyperf\ModelHashids\Concerns\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 `config/autoload/hashids.php`.

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 Hyperf\Database\Model\Model;
use FriendsOfHyperf\ModelHashids\Concerns\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 Hyperf\Database\Model\Model;
use FriendsOfHyperf\ModelHashids\Concerns\HasHashid;
use FriendsOfHyperf\ModelHashids\Concerns\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 Hyperf\Database\Model\Model;
use FriendsOfHyperf\ModelHashids\Concerns\HasHashid;
use FriendsOfHyperf\ModelHashids\Concerns\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();
```

Contact
-------

[](#contact)

- [Twitter](https://twitter.com/huangdijia)
- [Gmail](mailto:huangdijia@gmail.com)

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance71

Regular maintenance activity

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.8% 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 ~26 days

Recently: every ~6 days

Total

38

Last Release

170d ago

PHP version history (2 changes)v3.0.35PHP ^8.0

v3.1.0-beta.1PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8337659?v=4)[Deeka Wong](/maintainers/huangdijia)[@huangdijia](https://github.com/huangdijia)

---

Top Contributors

[![huangdijia](https://avatars.githubusercontent.com/u/8337659?v=4)](https://github.com/huangdijia "huangdijia (30 commits)")[![greezen](https://avatars.githubusercontent.com/u/6023468?v=4)](https://github.com/greezen "greezen (2 commits)")

---

Tags

hyperfv3.1

### Embed Badge

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

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

###  Alternatives

[hyperf/di

A DI for Hyperf.

182.8M594](/packages/hyperf-di)

PHPackages © 2026

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