PHPackages                             io238/eloquent-encoded-ids - 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. io238/eloquent-encoded-ids

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

io238/eloquent-encoded-ids
==========================

Automatic route key encryption for Laravel Eloquent using Hashids (short, unique, non-sequential ids) with prefix support

1.0.1(4y ago)951MITPHPPHP ^7.4|^8.0

Since Dec 30Pushed 1y ago1 watchersCompare

[ Source](https://github.com/io238/eloquent-encoded-ids)[ Packagist](https://packagist.org/packages/io238/eloquent-encoded-ids)[ Docs](https://github.com/io238/eloquent-encoded-ids)[ RSS](/packages/io238-eloquent-encoded-ids/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (5)Dependencies (5)Versions (6)Used By (0)

Eloquent Encoded IDs
====================

[](#eloquent-encoded-ids)

[![Latest Version on Packagist](https://camo.githubusercontent.com/0876548e657b61b06a5de04bb42f5e04786e640bf5fff07a803409e3973fe42d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696f3233382f656c6f7175656e742d656e636f6465642d6964732e7376673f6c6162656c3d56657273696f6e)](https://packagist.org/packages/io238/eloquent-encoded-ids)[![GitHub Tests Action Status](https://github.com/io238/eloquent-encoded-ids/workflows/Tests/badge.svg?branch=main)](https://github.com/io238/eloquent-encoded-ids/actions?query=workflow%3ATests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/d4f791093d4a6782e187ebd31f4e46ec74984e65f8fbd9b6379982cfc2fa7cc3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696f3233382f656c6f7175656e742d656e636f6465642d6964732e7376673f6c6162656c3d446f776e6c6f616473)](https://packagist.org/packages/io238/eloquent-encoded-ids)

Automatic route key encryption for Laravel Eloquent using [Hashids](https://hashids.org/) (short, unique, non-sequential IDs) with prefix support.

---

Background
----------

[](#background)

By default Laravel uses numeric, sequential IDs for models. These numeric IDs are then also used as *route keys*:

```
route('users.show', User::find(1)) // http://app.test/user/1
route('users.show', User::find(2)) // http://app.test/user/2
route('users.show', User::find(3)) // http://app.test/user/3
```

This package automatically encodes the model ID, so that sequence is not externally exposed:

```
route('users.show', User::find(1)) // http://app.test/user/m8y78
route('users.show', User::find(2)) // http://app.test/user/p8b7v
route('users.show', User::find(3)) // http://app.test/user/dvd6v
```

This is useful to hide sensitive app information (e.g. total number of users, invoices, etc).

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

[](#installation)

You can install the package via composer:

```
composer require io238/eloquent-encoded-ids
```

Usage
-----

[](#usage)

In order to encode the ID of a Laravel model, simply add the `HasEncodedIds` trait to the model:

```
namespace App\Models;

use Io238\EloquentEncodedIds\Traits\HasEncodedIds;

class User extends Model {

    use HasEncodedIds;

    // ..

}
```

Internally, Laravel will still use the numeric ID within controllers and also stores them as numeric IDs in the database:

```
class UserController extends Controller {

    public function show(User $user)
    {
        return $user->id; // 1
    }
}
```

### Prefixes

[](#prefixes)

By default this package adds a prefix to the encoded ID, which helps to identify what type of ID has been encoded.

**Example:** The `User` model has encoded IDs starting with `u_`, such as `u_m8y78`.

It uses the model name's first letter, or you can explicitly provide a prefix as a protected property of the model:

```
class User extends Model {

    use HasEncodedIds;

    protected $prefix = 'usr';

}
```

Config
------

[](#config)

This package works out-of-the-box. Nevertheless, you can publish and customize the config file with:

```
php artisan vendor:publish --provider="\Io238\EloquentEncodedIds\EncodedIdsProvider" --tag="config"
```

This is the contents of the default config file:

```
return [

    // Minimum length of encoded IDs
    'length'           => 5,

    // Alphabet to be used to generate encoded IDs
    // By default this list excludes ambiguous characters
    'alphabet'         => '123456789abcdefghikmnpqrstuvwxyz',

    // Ignore uppercase/lowercase for encoded IDs
    'case-insensitive' => true,

    // Encryption salt
    // Warning: changing the salt, will produce different encoded IDs
    'salt'             => env('APP_KEY'),

    // Use a prefix to the encoded ID, to be able to recognize the model that the ID belongs to
    'prefix'           => true,

    // Character used to separate the prefix from the encoded ID
    'separator'        => '_',

];
```

Testing
-------

[](#testing)

```
composer test
```

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

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

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

[](#security-vulnerabilities)

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

Credits
-------

[](#credits)

- [Martin Migge](https://github.com/io238)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

 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 ~111 days

Total

5

Last Release

1566d ago

Major Versions

0.2.0 → 1.0.02022-03-15

### Community

Maintainers

![](https://www.gravatar.com/avatar/5dba54660f9b9a054f9779084ce5c3a67f799e2be6829e37d9c1275aabf8e41c?d=identicon)[io238](/maintainers/io238)

---

Top Contributors

[![io238](https://avatars.githubusercontent.com/u/26689138?v=4)](https://github.com/io238 "io238 (35 commits)")

---

Tags

encryptionormeloquenthashidsidio238Route Key

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/io238-eloquent-encoded-ids/health.svg)

```
[![Health](https://phpackages.com/badges/io238-eloquent-encoded-ids/health.svg)](https://phpackages.com/packages/io238-eloquent-encoded-ids)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[veelasky/laravel-hashid

HashId Implementation on Laravel Eloquent ORM

46193.8k3](/packages/veelasky-laravel-hashid)

PHPackages © 2026

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