sirmathays / prefixed-id
Package for adding prefixes to model keys in Laravel.
Installs: 2 808
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- illuminate/database: ^7.0 || ^8.0 || ^9.0
- illuminate/support: ^7.0 || ^8.0 || ^9.0
Requires (Dev)
- mockery/mockery: ^1.3
- phpunit/phpunit: ^8.5
README
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="SirMathays\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}
withpid_
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 SirMathays\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.