james322 / has-nano-id-laravel
A php trait to add nano id to laravel models.
Fund package maintenance!
James Brown
Requires
- php: ^8.2
- hidehalo/nanoid-php: ^2.0
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
A php trait to add Nano IDs to Laravel models.
Installation
You can install the package via composer:
composer require james322/has-nano-id-laravel
Publish the config file with:
php artisan vendor:publish --tag="has-nano-id-laravel-config"
This is the contents of the published config file:
return [ 'alphabet' => env('NANO_ID_ALPHABET', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-'), 'size' => (int) env('NANO_ID_SIZE', 21) ];
Usage
In your model import the trait and add the column to your database table you wish to use for the Nano ID.
use james322\HasNanoId\HasNanoId; class User { use HasNanoId; }
Now when the model is created for the first time a Nano ID will be generated.
The database column used for storing the Nano ID can be customized by adding a $nano_id_key property on your model.
use james322\HasNanoId\HasNanoId; class User { use HasNanoId; public $nano_id_key = 'custom_column'; // default 'public_key' }
The default alphabet and size (length) of the nano id can be customized in the nano-id.php config file. You can also customize it on a per model basis by adding $nano_id_alphabet and $nano_id_size property to your model.
use james322\HasNanoId\HasNanoId; class User { use HasNanoId; public $nano_id_alphabet = 'abcdefg1234567890-'; // default '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-' public $nano_id_size = 14; // default 21 }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.