PHPackages                             touhidurabir/laravel-model-hashid - 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. touhidurabir/laravel-model-hashid

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

touhidurabir/laravel-model-hashid
=================================

A laravel package to generate model hashid based on model id column.

1.1.0(4y ago)13292MITPHPPHP &gt;=7.3

Since Aug 29Pushed 4y ago1 watchersCompare

[ Source](https://github.com/touhidurabir/laravel-model-hashid)[ Packagist](https://packagist.org/packages/touhidurabir/laravel-model-hashid)[ RSS](/packages/touhidurabir-laravel-model-hashid/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (11)Versions (3)Used By (0)

Laravel Model Hashid
====================

[](#laravel-model-hashid)

A package to generate model hash id from the model auto increment id for laravel models

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

[](#installation)

Require the package using composer:

```
composer require touhidurabir/laravel-model-hashid
```

To publish the config file:

```
php artisan vendor:publish --provider="Touhidurabir\ModelHashid\ModelHashidServiceProvider" --tag=config
```

How it can be helpful?
----------------------

[](#how-it-can-be-helpful)

When developing public APIs, sometimes we need to provide data based on givem model id from the requester endpoint . For example

```
/some-end-point/some-model-resource/{id}

```

where the give **{id}** can be model table associated auto increament id . Using an **uuid** is one another and now a days a pretty popular appraoch to obsecure the model table **id**. such as

```
/some-end-point/some-model-resource/{uuid}

```

But even with **uuid**, we do need to make adjustment to query to find model resource based on uuid or make the uuid the **Primary Key** of the model.

However this packaga take a different approach where on model resource creation , it gegerate and store an unique hash id from the model id and then use that to pass as response to remote request . And the requester use that hashid to make request to APIs instead of id or uuid but applying the middlewares, those hashid in the route param or request param dehashed to original model id . So basically this happens,

```
$id = 1;
$hashid = 'jRlef2';
```

when making the api request, like this

```
/some-end-point/some-model-resource/{hashid}

```

Say that route url point to some controller method, then

```
class SomeController extends Controller {

    public function show(int $id) {

        // $id where will be 1, not jRlef2
    }
}
```

Config Options
--------------

[](#config-options)

The published config file contails few possible configuration options . See and read through the **config/hasher.php** to get to know all the possible options . But few important ones are

### enable

[](#enable)

```
'enable' => env('ID_HASHING', false),
```

Determine if Hashid should be enabled or not . By default it is set to **true**.

### key

[](#key)

```
'key' => env('ID_HASHING_KEY', ''),
```

Use this unique key as the base or salt to generate the hash . It is not an required details but it is highly recommened to use one unique key through out the app to make the hashid stronger .

### column

[](#column)

```
'column' => 'hash_id',
```

The define which column by default this package should look for hashid to retrive or store the gererated one. But still possible to have some column name here and then have something different in some models.

### alphabets

[](#alphabets)

```
'alphabets' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
```

This defined the only characters that will be present in a hash string. Best To have along range of characters which by default is lower case a-z with upper case A-Z and 0-9.

> NOTE : it must be at least 16 characters long and must only contains unique characters. no duplicate allowed like 'aaaabbbbbbcc' etc .

### regeneration\_job

[](#regeneration_job)

```
'regeneration_job' => \Touhidurabir\ModelHashid\Jobs\ModelHashidRegeneratorJob::class,
```

This defined the job that will be used to run the update or fill up the missing hash id through the **hashid:run** command.

Command
-------

[](#command)

This packaga includes a command that can use to set up the model hash id for the missing ones or update existing one . It will be helpful if this package included later in any laravel app that already have some data in it's tables and needed to set up hash id for those records. To utlize this command run

```
php artisan hashid:run User,Profile
```

The one **argument** it reuired is the name of the models command seperated(if there is multiple models to run for) . Behind the scene it calls a queue job to go through the model recored and work on those to update/fill hash id column value. Other options as follow

### --path=

[](#--path)

By default it assumes all the models are located in the **App\\Models\\** namespace . But if it's located some where else , use the option to define the proper model space path with **trailing slash** .

### --update-all

[](#--update-all)

By default this command will only work with those model records that have the defined hash id column null . So basically it will fill up the missing ones , but if this false is provided with the command it will update all regardless of hashid associated with or not.

### --on-job

[](#--on-job)

This defined if this will update/fill missing one through a queue job . The command use a job where the main logic resides in . But by default it uses the framework provided **dispatchNow** method to run the jon in sync way . if the falg provided and queue configured properly, it will push the job in the queue .

### --job=

[](#--job)

If need to pass custom queue job implementation, it can be directly provided though this option . also one can update the queue class in the config file .

Usage
-----

[](#usage)

Use the trait **IdHashable** in model where uuid needed to attach

```
use Touhidurabir\ModelHashid\IdHashable;
use Illuminate\Database\Eloquent\Model;

class User extends Model {

    use IdHashable;
}
```

By default this package use the column name **hash\_id** to store the hash value of the model auto increment id.but this can be changed

Also possible to override the uuid column and attaching event from each of the model . to do that need to place the following method in the model :

```
use Touhidurabir\ModelHashid\IdHashable;
use Illuminate\Database\Eloquent\Model;

class User extends Model {

    use IdHashable;

    /**
     * Get the name of hash column name
     *
     * @return string
     */
    public function getHashColumn() {

        return 'hash';
    }
}
```

Now to make hashed route or request params automatically, use this following 2 middlewares

```
Touhidurabir\ModelHashid\Http\Middleware\DehashRequestParams // this to dehash request post/get params
Touhidurabir\ModelHashid\Http\Middleware\DehashRouteParams // this to dehash route hash params such as /{id} as hash
```

Register these middlewares in **Http\\Kernel.php** file or in controller/route file separately as require .

**NOTE** that the **DehashRequestParams** middleware can dehash request param that is **String(simple hash string)** or **Array(array of hash string)** only. So if require to handle complex param such as **JSON** string, need to handle that manually.

To handle such case where one need to decode some keys form a JSON response or for some other purpose need manual dehashing, this package provide 2 helper methods

- decode\_hashid
- decode\_hashids

As the name suggest, the **decode\_hashid** can only work with a single hash where **decode\_hashids** can work with single hash or array of hash.

Make sure to the put the hashid column name in migration file

```
$table->string('hash_id')->nullable()->unique()->index();
```

Or can be used with the combination of hasher config as such:

```
$table->string(config('hasher.column'))->nullable()->unique()->index();
```

This package also include some helper method that make it easy to find model records via UUID. for example

```
User::byHashId($hash)->where('active', true)->first(); // single hash id
User::byHashId([$hash1, $hash2])->where('active', true)->get(); // multiple hash id
```

Or simple and direct find

```
User::findByHashId($uhashuid); // single hash id
User::findByHashId([$hash1, $hash2]); // multiple hash id
```

> The package also provide a bit of safe guard by checking if the model table has the given hash id column .
>
> If the hash id column not found for model table schema, it will not create and attach an hash id.

Extras
------

[](#extras)

### For API resource

[](#for-api-resource)

One big use case of this package for the development of API services where gthe developers do not want to include the model original auto incrementing **id** . For that case, one should use the hash\_id as such

```
class User extends JsonResource {

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request) {

        return [
            'id'    => $this->hash_id,
            'email' => $this->email,
            ...
        ];
    }
}
```

The above approach is perfectly fine. Howeever as the hash id is not just used mostly for any other model related purpose , one may want to make it hidden like

```
/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'hash_id',
];
```

For such case, this package includes a simple trait to use with the api resource classes,

```
use Touhidurabir\ModelHashid\IdHashing;

class User extends JsonResource {

    use IdHashing;

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request) {

        return [
            'id'    => $this->getId(),
            'email' => $this->email,
            ...
        ];
    }
}
```

### Using the core Hasher class

[](#using-the-core-hasher-class)

The core of this package is the **Hasher** class that handle the whole hash **decode/encode** process. This is hightly dependent on the popular php [Hashid Package](https://github.com/vinkla/hashids) with some bit of extra functionality . One can also use this hasher as for their need fits . To see the details of the hasher class and how it works, check the code itself at **Touhidurabir\\ModelHashid\\Hasher\\Hasher** .

Contributing
------------

[](#contributing)

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License
-------

[](#license)

[MIT](./LICENSE.md)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity47

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

Every ~21 days

Total

2

Last Release

1696d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6c6009d764c48df4a55a8645c349f90a99c7e6e694b06c60b580e1da278d1db5?d=identicon)[touhidurabir](/maintainers/touhidurabir)

---

Top Contributors

[![touhidurabir](https://avatars.githubusercontent.com/u/2587979?v=4)](https://github.com/touhidurabir "touhidurabir (9 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/touhidurabir-laravel-model-hashid/health.svg)

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

###  Alternatives

[vinkla/hashids

A Hashids bridge for Laravel

2.1k13.3M73](/packages/vinkla-hashids)[illuminate/events

The Illuminate Events package.

13454.3M1.8k](/packages/illuminate-events)[illuminate/broadcasting

The Illuminate Broadcasting package.

7126.5M178](/packages/illuminate-broadcasting)[ashallendesign/short-url

A Laravel package for creating shortened URLs for your web apps.

1.4k1.9M4](/packages/ashallendesign-short-url)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)

PHPackages © 2026

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