Download the PHP package sirmathays/prefixed-id without Composer
On this page you can find all versions of the php package sirmathays/prefixed-id. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sirmathays/prefixed-id
More information about sirmathays/prefixed-id
Files in sirmathays/prefixed-id
Package prefixed-id
Short Description Package for adding prefixes to model keys in Laravel.
License MIT
Informations about the package prefixed-id
Prefixed IDs
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:
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:
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
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
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
You can change the route binding name in the config.
ResourceController.php
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:
The find methods would return instances of App\Models\Human
and App\Models\Dog
respectively.
All versions of prefixed-id with dependencies
illuminate/database Version ^7.0 || ^8.0 || ^9.0
illuminate/support Version ^7.0 || ^8.0 || ^9.0