lyhty/prefixed-id

Package for adding prefixes to model keys in Laravel.
8,132
Install
composer require lyhty/prefixed-id
Latest Version:v2.2.0
PHP:>=7.4
License:MIT
Last Updated:Mar 12, 2023
Links: GitHub  ·  Packagist
Maintainer: TruecapeDev

Latest Version on Packagist PHP Laravel Total Downloads License

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

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

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

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

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

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

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.

Related Packages

mitchdav/laravel-stripe-ids

Generate Stripe-style IDs (ch_xxxxxxxxxx) for your Eloquent models.

1,669 1
moharrum/laravel-eloquent-extras

Set of extra and useful Eloquent helpers to speed up development.

65 0
moharrum/generates-uuids

Generate v4 UUIDs for eloquent primary key column on model creation.

30 0
kodeine/laravel-meta

Fluent Meta Data for Eloquent Models, as if it is a property on your model.

923,509 422