PHPackages                             bvtterfly/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. [Database &amp; ORM](/categories/database)
4. /
5. bvtterfly/laravel-hashids

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

bvtterfly/laravel-hashids
=========================

A package to generate hashids for Eloquent models

1.1.0(3y ago)2815.1k4[2 PRs](https://github.com/bvtterfly/laravel-hashids/pulls)MITPHPPHP ^8.0

Since Apr 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/bvtterfly/laravel-hashids)[ Packagist](https://packagist.org/packages/bvtterfly/laravel-hashids)[ Docs](https://github.com/bvtterfly/laravel-hashids)[ RSS](/packages/bvtterfly-laravel-hashids/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (3)Dependencies (13)Versions (6)Used By (0)

🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨

I no longer use Laravel and cannot justify the time needed to maintain this package. That's why I have chosen to abandon it. Feel free to fork my code and maintain your own copy.

Laravel Hashids
===============

[](#laravel-hashids)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f844453a982d8a92f38280aeb9ad90fb06cb201571941134599e6ac96dbf2818/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f627674746572666c792f6c61726176656c2d686173686964732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bvtterfly/laravel-hashids)[![run-tests](https://github.com/bvtterfly/laravel-hashids/actions/workflows/run-tests.yml/badge.svg)](https://github.com/bvtterfly/laravel-hashids/actions/workflows/run-tests.yml)[![Check & fix styling](https://github.com/bvtterfly/laravel-hashids/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/bvtterfly/laravel-hashids/actions/workflows/php-cs-fixer.yml)[![Total Downloads](https://camo.githubusercontent.com/d793c456099a3c895f82c424105ca9332c3bb97c20f977ad7be293c7b96650f5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f627674746572666c792f6c61726176656c2d686173686964732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bvtterfly/laravel-hashids)

This package provides a trait that will generate hashids when saving any Eloquent model.

Hashids
-------

[](#hashids)

[Hashids](https://github.com/vinkla/hashids) is a small package to generate YouTube-like IDs from numbers. It converts numbers like `347` into strings like `yr8`.

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

[](#installation)

You can install the package via composer:

```
composer require bvtterfly/laravel-hashids
```

You can publish the config file with:

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

This is the contents of the published config file:

```
return [
    /*
    |--------------------------------------------------------------------------
    | Default Salt
    |--------------------------------------------------------------------------
    |
    | This is the salt that uses by Hashids package to generate unique id.
    |
    */
    'salt' => config('app.name')
];
```

Usage
-----

[](#usage)

Your Eloquent models should have the ` Bvtterfly\LaravelHashids\HasHashId` trait that contains an abstract `getHashIdOptions` method that you must implement yourself, and it should return the `Bvtterfly\LaravelHashids\HashIdOptions` class.

Your models' migrations should have a field to save the generated hashid to.

Here's an example of what a model would look like:

```
namespace App\Models;

use Bvtterfly\LaravelHashids\HasHashId;
use Bvtterfly\LaravelHashids\HashIdOptions;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasHashId;

    public function getHashIdOptions(): HashIdOptions
    {
        return HashIdOptions::create()->saveHashIdTo('hashid');
    }

}
```

> By default, Package will generate hashids from models' `id`.

And Its migration:

```
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('hashid')->nullable(); // Field name same as your `saveHashIdTo`
            //...
            $table->timestamps();
        });
    }
}
```

> The `hashid` column is generated from the `id` field, But `id` is an auto-increment column and doesn't have value before saving in the DB. So, The `hashid` column must be nullable. And The Package will generate `hashid` and update the model after being saved in the database.

### Generate from Hex numbers

[](#generate-from-hex-numbers)

If you want to generate hashids from hex numbers like [Mongo](https://www.mongodb.com)'s ObjectIds, you can change the type to the `hex`:

```
public function getHashIdOptions(): HashIdOptions
{
    return HashIdOptions::create()
        ->saveHashIdTo('hashid')
        ->setType('hex') // default = int
    ;
}
```

### Generate from another field

[](#generate-from-another-field)

```
public function getHashIdOptions(): HashIdOptions
{
    return HashIdOptions::create()
        ->saveHashIdTo('hashid')
        ->generateHashIdFrom('custom_key')

    ;
}
```

### Generate from field with value

[](#generate-from-field-with-value)

By default, This package will generate hashids and update the model from the auto-incremented `id` column after being saved in the database. Still, if your field has value, you can change it to generate hashids while saving:

```
public function getHashIdOptions(): HashIdOptions
{
    return HashIdOptions::create()
        ->saveHashIdTo('hashid')
        ->setAutoGeneratedField(false)
    ;
}
```

### Use the same hashids among models

[](#use-the-same-hashids-among-models)

The package will add the models' table to the default salt to generate a unique output id per model. If you want your `Post` and `User` models to share the same output id when `id = 1`:

```
public function getHashIdOptions(): HashIdOptions
{
    return HashIdOptions::create()
        ->saveHashIdTo('hashid')
        ->setGenerateUniqueHashIds(false)
    ;
}
```

### Use padding to make your output ids longer

[](#use-padding-to-make-your-output-ids-longer)

Without padding, encoding of `1` returns something like `jR`, but You can use padding to have a longer output id.

> Note that output ids are only padded to fit **at least** a certain length. It doesn't mean that they will be *exactly* that length.

```
public function getHashIdOptions(): HashIdOptions
{
    return HashIdOptions::create()
        ->saveHashIdTo('hashid')
        ->setMinimumHashLength(10)
    ;
}
```

### Using a custom alphabet

[](#using-a-custom-alphabet)

```
public function getHashIdOptions(): HashIdOptions
{
    return HashIdOptions::create()
        ->saveHashIdTo('hashid')
        // use all lowercase alphabet instead of 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
        ->setAlphabet('abcdefghijklmnopqrstuvwxyz')
    ;
}
```

### Using Hashids in routes

[](#using-hashids-in-routes)

To use the hashids in routes, you may specify the hashid column in the route parameter definition:

```
use App\Models\Post;

Route::get('/posts/{post:hashid}', function (Post $post) {
    return $post;
});
```

or If you would like model binding to always use the hashid column other than id when retrieving a given model class, you may override the getRouteKeyName method on the Eloquent model:

```
/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'hashid';
}
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [ARI](https://github.com/bvtterfly)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% 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 ~156 days

Total

3

Last Release

1210d ago

Major Versions

0.1.0 → 1.0.02022-06-02

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/99682351?v=4)[Λгi](/maintainers/bvtterfly)[@bvtterfly](https://github.com/bvtterfly)

---

Top Contributors

[![bvtterfly](https://avatars.githubusercontent.com/u/99682351?v=4)](https://github.com/bvtterfly "bvtterfly (17 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (9 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (8 commits)")

---

Tags

eloquentlaravellaravelLaravel-hashidsbvtterfly

###  Code Quality

TestsPest

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.3M42](/packages/spatie-laravel-pdf)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

213389.8k2](/packages/wnx-laravel-backup-restore)[spatie/laravel-model-flags

Add flags to Eloquent models

4471.2M4](/packages/spatie-laravel-model-flags)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

438834.4k1](/packages/clickbar-laravel-magellan)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3913.7k](/packages/rawilk-profile-filament-plugin)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17555.1k](/packages/lacodix-laravel-model-filter)

PHPackages © 2026

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