james322/has-nano-id-laravel

A php trait to add nano id to laravel models.

0.0.2 2024-11-13 23:05 UTC

This package is auto-updated.

Last update: 2024-11-13 23:07:34 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.