1. Go to this page and download the library: Download malico/laravel-nanoid library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
malico / laravel-nanoid example snippets
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;
}
public function up(): void
{
Schema::create('books', function (Blueprint $table) {
$table->string('id')->primary();
$table->timestamps();
});
}
// One prefix for all generated ids
protected $nanoidPrefix = 'p-';
// Per-column prefixes
protected $nanoidPrefix = ['id' => 'p-', 'username' => 'u_'];
// 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]];
// One alphabet for all generated ids
protected $nanoidAlphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
// Per-column alphabets
protected $nanoidAlphabet = [
'id' => 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789',
'username' => 'abcdefghijklmnopqrstuvwxyz',
];
// One format for all unique id columns
protected $nanoidFormat = 'u_{4}-{4}';
// Per-column formats
protected $nanoidFormat = ['id' => 'p-{7}', 'username' => 'u_{4}-{4}'];
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
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
bash
php artisan make:nanoid-migration
bash
php benchmarks/nanoid.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.