Compatible with laravel version 5.8.x
This package allows you to use hashids in your models using the vinkla/laravel-hashids package
This package includes the vinkla/laravel-hashids package
You can install the package via composer:
composer require jaacu/laravel-model-hashids
Add to your model migration the following code
...
$table->hashid();
...
Then simply add the UsesHashIds trait to your model
use Jaacu\LaravelModelHashids\Traits\UsesHashIds;
class MyModel extends Model
{
use UsesHashIds;
}
Now using default laravel routing implicint binding will use the hashid insted of the normal id
A model using the trait UsesHashIds
$model = Model::first();
Generating url
route('model.show',$model); // output: http://mydomain/model/<hashid>
Getting the hashid
$model->getId() // returns: the model <hashid>
Publish the config file to change default behavior
php artisan vendor:publish --provider="Jaacu\LaravelModelHashids\LaravelModelHashidsServiceProvider"
By default the generated hashid follows this structure: app name + model name + model id hash
- app name is set by the app.config name and can be overwritten by setting a new 'short_name' in the config/app.php file
- model name is set by the model class name
- model hash id is set by the model 'id' hash
This package shares the config file from vinkla/laravel-hashids
In the config/hashids.php you can set the hash length and salt.
'connections' => [
'main' => [
'salt' => env('APP_KEY'), // Uses the env app key by default
'length' => 6,
],
'alternative' => [
'salt' => 'your-salt-string',
'length' => 'your-length-integer',
],
]
For more info on the hash functionality see vinkla/laravel-hashids
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email jaacu.97@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.