PHPackages                             lyhty/prefixed-id - 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. lyhty/prefixed-id

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

lyhty/prefixed-id
=================

Package for adding prefixes to model keys in Laravel.

v2.2.0(3y ago)06.1k↓32.1%1MITPHPPHP &gt;=7.4

Since May 14Pushed 3y agoCompare

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

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

 [![](https://camo.githubusercontent.com/2683e1dd601da1f298f339bdbb616a3dff73d10ed7185010bb8b09340213ae78/68747470733a2f2f6d617474692e73756f72616e69656d692e636f6d2f73746f726167652f6c796874792d70726566697865642d69642e706e67)](https://camo.githubusercontent.com/2683e1dd601da1f298f339bdbb616a3dff73d10ed7185010bb8b09340213ae78/68747470733a2f2f6d617474692e73756f72616e69656d692e636f6d2f73746f726167652f6c796874792d70726566697865642d69642e706e67)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a54b2402e000c9b453747499ae5e12b475f5eb7ecf2fee1b8cdad45d20a26ac4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c796874792f70726566697865642d69642e7376673f6c6162656c3d267374796c653d666c61742d737175617265266c6f676f3d7061636b6167697374266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/lyhty/prefixed-id)[![PHP](https://camo.githubusercontent.com/bdb67bd33b89ecaeb0f8265a675900e04e6a7eb1708bf7bc1c285e93ce3281b2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c796874792f70726566697865642d69643f7374796c653d666c61742d737175617265266c6162656c3d266c6f676f3d706870266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/lyhty/prefixed-id)[![Laravel](https://camo.githubusercontent.com/90fcd9a2c6268779b07f79d72fecb622714d85e31e6c1a09fb3fa9033b3008dd/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d266d6573736167653d253545382e302532302e2e2e25323025354531302e3026636f6c6f723d726564267374796c653d666c61742d737175617265266c6f676f3d6c61726176656c266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/lyhty/prefixed-id)[![Total Downloads](https://camo.githubusercontent.com/bd4e822f4c041e7dd598d93111bbaed114663f072907cd77f0e7583aca1caaa1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c796874792f70726566697865642d69642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lyhty/prefixed-id)[![License](https://camo.githubusercontent.com/897bc5a807508e24946e2b459db5abc1669ad921821c5caebaa7123fdf6de23b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c796874792f70726566697865642d69642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lyhty/prefixed-id)

This package will provide models the ability to have a prefixed id in your system, while still having the database value being just the usual id.

Config
------

[](#config)

Run following command to publish the config file:

```
php artisan vendor:publish --provider="Lyhty\PrefixedId\PrefixedIdServiceProvider"
```

You should set up the models that you want to have ids with prefixes, you should also apply HasPrefixedId trait to the model.

Example
-------

[](#example)

Model with a prefixed id would work the following way:

```
use App\Models\Human;

$human = Human::pidFindOrFail("H-1");
$human->pid; // Prints "H-1"
$human->id; // Prints 1
```

Route binding
-------------

[](#route-binding)

The package automatically binds the models set up in the config file to use prefixes in routing.

### Model specific binding

[](#model-specific-binding)

A model with a prefixed id set up would behave in a following way: `project.test/humans/H-1`

**web.php**

```
use Illuminate\Support\Facades\Route;

Route::get('humans/{human}', [HumanController::class, 'show']);
```

> You can apply an optional prefix to the binding in the config. E.g. `humans/{pid_human}` with `pid_` being the applied prefix. This would keep the Laravel's default binding to the model's key without the prefix.

**HumanController.php**

```
public function show(Request $request, Human $human)
{
    //
}
```

As you can see, setting up the model binding works the same as without the prefixed id.

### Generic model binding

[](#generic-model-binding)

The package also provides more generic routing. An example follows:

**web.php**

```
use Illuminate\Support\Facades\Route;

Route::get('resources/{prefixedModel}', [ResourceController::class, 'show']);
```

> You can change the route binding name in the config.

**ResourceController.php**

```
public function show(Request $request, Model $model)
{
    //
}
```

With a route setup like this, both `project.test/resources/H-1` and `project.test/resources/D-1` would work and would return a Human model instance and a Dog model instance respectively.

This way you can have one route for returning any type of a model, as long as it is set up in config.

Prefixed id helper class
------------------------

[](#prefixed-id-helper-class)

You can utilize PrefixedId class to find instances in a following way:

```
use Lyhty\PrefixedId\Facades\PrefixedId;

$human = PrefixedId::findModel('H-1');
$dog = PrefixedId::findModel('D-1');
```

The find methods would return instances of `App\Models\Human` and `App\Models\Dog` respectively.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

Recently: every ~161 days

Total

9

Last Release

1162d ago

Major Versions

v1.3.1 → v2.02022-02-28

### Community

Maintainers

![](https://www.gravatar.com/avatar/2cd5d6f2caaea83278fd1552c5bdc1b30b4164890ba0801a718dc20efa93738c?d=identicon)[TruecapeDev](/maintainers/TruecapeDev)

---

Top Contributors

[![sirmathays](https://avatars.githubusercontent.com/u/37704147?v=4)](https://github.com/sirmathays "sirmathays (17 commits)")

---

Tags

laravelmodelprefixid

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lyhty-prefixed-id/health.svg)

```
[![Health](https://phpackages.com/badges/lyhty-prefixed-id/health.svg)](https://phpackages.com/packages/lyhty-prefixed-id)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)[sebastiaanluca/laravel-boolean-dates

Automatically convert Eloquent model boolean attributes to dates (and back).

40111.7k1](/packages/sebastiaanluca-laravel-boolean-dates)

PHPackages © 2026

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