amirkacem / model-key-factory
A simple package to generate Eloquent Model Keys
v1.0.1
2022-04-08 23:17 UTC
Requires
- php: ^8.0
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.4.3
Requires (Dev)
- nunomaduro/collision: ^6.1
- orchestra/testbench: ^7.3
- phpunit/phpunit: ^9.5
README
Installation
composer require amirkacem/model-key-factory
You can publish the config file with:
php artisan vendor:publish --provider="Eloquent\KeyFactory\KeyFactoryServiceProvider" --tag="model-key-factory-config"
This is the contents of the published config file:
return [ 'key' => [ 'length' => 20, ] ];
Usage
This is a very simple to use package. There are two available methods to generate the key:
Using the Key Factory
$key = \Eloquent\KeyFactory\KeyFactory::generate( prefix: 'tests', // what you want to prefix your keys with. length: 20, // optional - the default of 20 is set in the config. );
Using the Str helper
$key = \Illuminate\Support\Str::key( prefix: 'tests', // what you want to prefix your keys with. length: 20, // optional - the default of 20 is set in the config. );
Test
vendor/bin/phpunit
Eloquent Integration
There is an eloquent model trait available to use called HasKey
which will:
When you are creating an eloquent model, the trait will be booted. It will get the first 3 characters of the Model name, force them to lowercase and append a "_" and use this as the prefix for the Key Factory