Download the PHP package antoninmasek/laravel-model-hashids without Composer
On this page you can find all versions of the php package antoninmasek/laravel-model-hashids. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download antoninmasek/laravel-model-hashids
More information about antoninmasek/laravel-model-hashids
Files in antoninmasek/laravel-model-hashids
Package laravel-model-hashids
Short Description Easily use Hashids with Laravel models.
License MIT
Homepage https://github.com/antoninmasek/laravel-model-hashids
Informations about the package laravel-model-hashids
Easily use Hashids with Laravel models.
In some cases I really like to use Hashids instead of uuids as my model keys. For me Hashids are less awkward to read and the resulting URL looks a bit nicer in my opinion. This package is inspired by laravel-model-uuid by Michael Dyrynda and aims to make it a breeze to start using Hashids as your model keys.
Installation
You can install the package via composer:
You can publish the config file with:
This is the contents of the published config file:
This package uses antoninmasek/laravel-hashids in the background. And if you wish to configure some aspects of the underlying hash id generation, then please take a look at a readme of the package.
Usage
To use this package you'll be most interested in the following two traits: GeneratesHashId
and BindsOnHashId
.
Generating hash id
In order for your model to automatically get a hash id after it is created just use
GeneratesHashId
trait on your model:
Binding on hash id
To also bind your model to hash id you also need to use BindsOnHashId
trait:
Configuration
If you need to execute some logic in order to determine salt/alphabet/minlength you have a few callbacks at your disposal:
Global
If you want to set these globally you may use the following callbacks. The callback is supplied with the model as a parameter.
Local
If you wish to have a specific logic just for a certain model you may define these methods on the desired model:
Precedence
This is the order in which the values are taken:
- Model specific logic
- Global logic
- Config values
- Hashids package
Regenerating hash id
If you wish to regenerate a hash id for a particular model with current configuration you may do so as follows:
Limitations
If your model key is auto-incrementing then, at least at the moment, there are 2 round-trips to the database. 1st to create the model and receive the ID and then 2nd to set the hash_id based on the ID.
Note:
Updating
eloquent event is not fired, when setting thehash_id
. This is the default behaviour since version 0.6.0. It is still possible to change this viasave_quietly
config setting.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
Special thanks
- To Michael Dyrynda for his laravel-model-uuid package, by which this package is heavily inspired.
- To Spatie for their amazing skeleton which I used to scaffold this repository.
All versions of laravel-model-hashids with dependencies
antoninmasek/laravel-hashids Version ^1.0.0
illuminate/contracts Version ^10.0 || ^11.0
spatie/laravel-package-tools Version ^1.14.1