PHPackages                             funfirst/laravel-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. funfirst/laravel-hashids

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

funfirst/laravel-hashids
========================

HashId Implementation on Laravel Eloquent ORM

018PHP

Since Nov 25Pushed 1y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel HashId
--------------

[](#laravel-hashid)

[![Test](https://github.com/veelasky/laravel-hashid/workflows/Test/badge.svg)](https://github.com/veelasky/laravel-hashid/workflows/Test/badge.svg)[![Codacy Badge](https://camo.githubusercontent.com/07ac49a029919cb41db9bbc01b1416c39e2ea4b2bfc0a770145a75ab5fd6a619/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3365393239623533323761393435336262306461356362663265636238373934)](https://app.codacy.com/gh/veelasky/laravel-hashid?utm_source=github.com&utm_medium=referral&utm_content=veelasky/laravel-hashid&utm_campaign=Badge_Grade)[![codecov](https://camo.githubusercontent.com/79417fbc33dfb2d781536c894a1f109947abae8feae4e4f5e043b7a579ae0c1e/68747470733a2f2f636f6465636f762e696f2f67682f7665656c61736b792f6c61726176656c2d6861736869642f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d743935796d734d794458)](https://codecov.io/gh/veelasky/laravel-hashid)[![Latest Stable Version](https://camo.githubusercontent.com/096f2617478fc860725660f351f25f924dbc49467f4daefd3cb8192e2c866926/68747470733a2f2f706f7365722e707567782e6f72672f7665656c61736b792f6c61726176656c2d6861736869642f76)](//packagist.org/packages/veelasky/laravel-hashid)[![StyleCI](https://camo.githubusercontent.com/3982ed2d9870fff48d035dc77f64ce91e6a4533910b3bd5d536c8affafa8beb3/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3131383432343634332f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/118424643?branch=master)[![Total Downloads](https://camo.githubusercontent.com/e9f2689f83ab7277f9a474fcff526fb875fb337e97b043ddfd236ebbc82cf651/68747470733a2f2f706f7365722e707567782e6f72672f7665656c61736b792f6c61726176656c2d6861736869642f646f776e6c6f616473)](//packagist.org/packages/veelasky/laravel-hashid)[![Dependents](https://camo.githubusercontent.com/d3f51e23bf67404bdbbf04e4be4b4b0b140940d91ac73194adfe52e1ccfcbb14/68747470733a2f2f706f7365722e707567782e6f72672f7665656c61736b792f6c61726176656c2d6861736869642f646570656e64656e7473)](//packagist.org/packages/veelasky/laravel-hashid)[![License](https://camo.githubusercontent.com/6299c0507a10be00420e2fb2b7bc98e932c013b811dd8a21719f34578324914f/68747470733a2f2f706f7365722e707567782e6f72672f7665656c61736b792f6c61726176656c2d6861736869642f6c6963656e7365)](//packagist.org/packages/veelasky/laravel-hashid)

Automatic HashId generator for your eloquent model.

### Version Compatibilities

[](#version-compatibilities)

Laravel HashIdPHP VersionLaravel 5.\*Laravel 6.\*Laravel 7.\*Laravel 8.\*Laravel 9.\*Laravel 10.\*`1.x``>=7.0`✅✅❌❌❌❌`2.x``>=7.2` - `=7.4` || `>= 8.0`❌✅✅✅✅❌`3.1``>= 8.0`❌✅✅✅✅✅`4.x``>= 8.1`❌❌❌❌❌✅### Install

[](#install)

```
composer require veelasky/laravel-hashid
```

With laravel package auto discovery, this will automatically add this package to your laravel application.

### TLDR

[](#tldr)

Simply add `HashableId` trait on any of your eloquent model you are intending to use with HashId.

Example:

```
use Illuminate\Database\Eloquent\Model;
use Veelasky\LaravelHashId\Eloquent\HashableId;

class User extends Model {
    use HashableId;
    ...
}
```

### Usage

[](#usage)

#### With Eloquent Model

[](#with-eloquent-model)

```
$user = User::find(1);     // instance of user.
$user->hash;               // generate HashId.

// Database operation

// get user by hashed id.
$user = User::byHash($hash);

// get user by hashed id, and throw ModelNotFoundException if not present.
$user = User::byHashOrFail($hash);

// get hashed id from the primary key.
User::idToHash($id);

// get ID from hashed string.
User::hashToId($hash);

 // query scope with `byHash` method.
User::query()->byHash($hash);
```

By default, all hash calculation will be calculated at runtime, but sometime you want to persist the hashed id to the database.

> NOTE: when using persisting model, all database query will be check againts the table itself, except: `$model->hash` will always be calculated at runtime.

```
class User extends Model {
    use HashableId;

    // add this property to your model if you want to persist to the database.
    protected $shouldHashPersist = true;

    // by default, the persisted value will be stored in `hashid` column
    // override column name to your desired name.
    protected $hashColumnName = 'hashid';
    ...
}
```

#### Salt

[](#salt)

The salt is generated automatically based on your app key and hash\_alphabet. If you need to use the same salt between different projects, you can set the `HASHID_SALT` environment variable.

#### Route binding

[](#route-binding)

When HashableId trait is used, base `getRouteKey()` and `resolveRouteBinding()` are overwritten to use the HashId as route key.

```
use App\Models\User;

class UserController extends Controller
{
    /**
     * Route /users/{user}
     * Ex: GET /users/k1jTdv6l
     */
    public function show(User $user)
    {
        ...
    }
}
```

#### In-Depth Coverage

[](#in-depth-coverage)

This package use repository pattern to store all instantiated implementation of `HashId\HashId` class, this to achieve different hash result on every eloquent models defined with `HashableId` trait.

```
// using facade.
HashId::hashToId($hash, User::class)      // same as User::hashToId($hash);
HashId::idToHash($id, User::class)        // same as User::idToHash($hash);

// HashId facade class is an implementation of \Veelasky\Laravel\HashId\Repository
```

However you can opt-out to not using any eloquent model or implementing your own logic to the repository.

```
HashId::make($key, $salt);              // will return \HashId\HashId class.

// once you instantiated the object, you can retrieve it on your next operation
HashId::get($key);
```

If you're using single table inheritance model, where you want to has the same calculated hash across all inherited models, use `$hashKey` property, this will result the calculation remain the same across all inherited model.

```
class User extends Model {
    protected $hashKey = 'somethingUnique';
}

class Customer extends User {

}

$customer = Customer::find(1);
$user = User::find(1);

$user->hash; // will be equal to $customer->hash
```

You can also specify the length and characters of the hashed Id with `HASHID_LENGTH` and `HASHID_ALPHABET` environment variable respectively, or you can publish the configuration file using this command:

```
php artisan vendor:publish --tag=laravel-hashid-config
```

#### Extra: Validation Rules

[](#extra-validation-rules)

You can also use this as validation rules, simply add this rule to your validator.

```
use App\Models\User;
use Veelasky\LaravelHashId\Rules\ExistsByHash;

...
Validator::make([
    'id' => $hashedId
], [
    'id' => ['required', new ExistsByHash(User::class)],
]);
...
```

#### License

[](#license)

MIT License

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity16

Early-stage or recently created project

 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.

### Community

Maintainers

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

---

Top Contributors

[![CodeSkills](https://avatars.githubusercontent.com/u/3975293?v=4)](https://github.com/CodeSkills "CodeSkills (14 commits)")

### Embed Badge

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

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

PHPackages © 2026

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