malico/laravel-nanoid is a Laravel package.
It currently has 45 GitHub stars and 126.024 downloads on Packagist (latest version 1.4.2).
Install it with composer require malico/laravel-nanoid.
Discover more Laravel packages by malico
or browse all Laravel packages to compare alternatives.
Last updated
Generate Nanoid-based, Stripe-style IDs for your Eloquent models.
composer require malico/laravel-nanoid
Add HasNanoids to your model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Malico\LaravelNanoid\HasNanoids;
class Book extends Model
{
use HasFactory;
use HasNanoids;
}
Use a string primary key in your migration:
public function up(): void
{
Schema::create('books', function (Blueprint $table) {
$table->string('id')->primary();
$table->timestamps();
});
}
You can also scaffold migrations with:
php artisan make:nanoid-migration
make:nanoid-migration accepts the same arguments as make:migration.
You can configure generated IDs per model with these properties (or methods with the same names):
nanoidPrefix: static prefix, for example ord_nanoidLength: fixed length (10) or random range ([10, 20])nanoidAlphabet: allowed charactersnanoidFormat: structured format like {3}-{4}Each option supports either:
nanoidPrefix// One prefix for all generated ids
protected $nanoidPrefix = 'p-';
// Per-column prefixes
protected $nanoidPrefix = ['id' => 'p-', 'username' => 'u_'];
nanoidLength// One fixed length for all generated ids
protected $nanoidLength = 12;
// Random length between min and max for all generated ids
protected $nanoidLength = [2, 5];
// Per-column fixed length
protected $nanoidLength = ['id' => 7, 'username' => 12];
// Per-column ranged length (min, max)
protected $nanoidLength = ['id' => [2, 5], 'username' => [8, 10]];
// Invalid: range must have exactly two values [min, max]
protected $nanoidLength = ['id' => [2]];
nanoidAlphabet// One alphabet for all generated ids
protected $nanoidAlphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
// Per-column alphabets
protected $nanoidAlphabet = [
'id' => 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789',
'username' => 'abcdefghijklmnopqrstuvwxyz',
];
nanoidFormat// One format for all unique id columns
protected $nanoidFormat = 'u_{4}-{4}';
// Per-column formats
protected $nanoidFormat = ['id' => 'p-{7}', 'username' => 'u_{4}-{4}'];
<?php
use Illuminate\Database\Eloquent\Model;
use Malico\LaravelNanoid\HasNanoids;
class SessionToken extends Model
{
use HasNanoids;
protected $nanoidAlphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
protected $nanoidPrefix = [
'id' => 'p-',
];
protected $nanoidLength = 12;
protected $nanoidFormat = [
'username' => 'u_{8}',
];
public function uniqueIds(): array
{
return ['id', 'username'];
}
}
// id: p-8F4Z2K9T7Q1M
// username: u_A8K9P2QW
Use nanoidFormat when you want readable, segmented IDs.
Supported placeholders:
{n} generates exactly n random characters{min-max} generates a random length between min and maxAll other characters are kept as-is (-, _, ., spaces, and so on).
class TrackingCode extends Model
{
use HasNanoids;
protected $nanoidFormat = 'TRK-{3}-{3-4}-{6}';
}
// TRK-X9a-k2Pm-8Qw1Zr
// TRK-L0p-r7A-2bV9tK
class Coupon extends Model
{
use HasNanoids;
protected $nanoidAlphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
protected $nanoidFormat = '{4} {4}';
}
// 7KQ2 M9TZ
Rules:
nanoidFormat is set, it fully controls the generated shapenanoidFormat cannot be combined with nanoidPrefixnanoidFormat cannot be combined with nanoidLengthnanoidFormat, generation falls back to nanoidPrefix + nanoidLengthRun the included micro-benchmark:
php benchmarks/nanoid.php
p-8F4Z2K9T7Q1M)HasUlids when you want sortable IDs that preserve creation order betterIf your system requires strictly incremental IDs, this package is not the right tool.
If you are upgrading from 0.x, see UPGRADE.MD.
Ndifon Desmond Yong