jaacu/laravel-model-hashids

1.0.0 2019-05-18 01:10 UTC

This package is auto-updated.

Last update: 2024-10-18 13:32:32 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads StyleCI

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

Installation

You can install the package via composer:

composer require jaacu/laravel-model-hashids

Usage

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

Examples

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>

Config

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',
    ],
]

Docs

For more info on the hash functionality see vinkla/laravel-hashids

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email jaacu.97@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.