malico / laravel-nanoid
Installs: 14 020
Dependents: 0
Suggesters: 0
Security: 0
Stars: 37
Watchers: 1
Forks: 5
Open Issues: 0
Requires
- hidehalo/nanoid-php: ^1.1
- illuminate/auth: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- ext-pdo: *
- laravel/pint: ^1.10
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
README
Introduction
A simple drop-in solution for providing nanoid support for the IDs of your Eloquent models. (Stripe-like IDs)
Installing
composer require malico/laravel-nanoid
Usage
Use nanoid within your model, use the trait HasNanoids
within your model like.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; + use Malico\LaravelNanoid\HasNanoids; class Book extends Model{ use HasFactory; + use HasNanoids; }
Your migration file should like.
// migration file public function up() { Schema::create('test_models', function (Blueprint $table) { - $table->id(); + $table->string('id')->primary(); // $table->timestamps(); }); }
To create a new migration, use the artisan command make:nanoid-migration
. All arguments work the same as the make:migration
command.
Options
- Prefix: To Specify a Prefix for the IDs, you can specify a prefix by add `nanoPrefix' property to your model class.
- Same applies for the length of the ID.
<?php class YourModel Extends \Illuminate\Database\Eloquent\Model { /** @var array|int */ protected $nanoidLength = 10; // id will be of length 10 // specifying to array. e.g [10, 20] will generate id of length 10 to 20 // or public function nanoidLength(): array|int { // [10,20] return 10; } /** @var string */ protected $nanoidPrefix = 'pl_'; // id will look: pl_2k1MzOO2shfwow ... // or public function nanoidPrefix(): string { return 'pay_'; // pay_2MII83829sl2d } /** @var string */ protected $nanoidAlphabet = 'ABC'; // or public function nanoidAlphabet(): string { return 'ABC'; // pay_ACBACB } public function uniqueIds() { // will create nanonids for 'unique_id' &'another_with' // Also, won't break if id is not listed. return ['unique_id', 'another_id']; } }
Check the upgrade guide if you're upgrading from 0.x
Author
Ndifon Desmond Yong