You can install the package via composer:
composer require vanthao03596/laravel-ulid
Generate uld from Str support class
// Default ulid generator with the current timestamp & lowercase string
Str::ulid(); // 01FDRXQ57VR4K4RASPT9NPAQC0
// Default ulid generator with the current timestamp & uppercase string
Str::ulid(false); // 01fdrxpmg9njp20z3km461fgax
// Default ulid generator with the given datetime & uppercase string
Str::ulid(false, Carbon::now()->subHour()) // 01FDRTD2K8Z664S24X606N14KD
Simply declare a ulid column type in your migration files.
Schema::create('foos', function (Blueprint $table) {
$table->ulid('id')->primary(); // adds primary ulid column
$table->foreignUlid('user_id')->constrained()->cascadeOnDelete(); // adds ulid foreignkey
$table->ulidMorphs('taggable'); // adds ulid morphs
$table->nullableUlidMorphs('likeable'); // adds nullable ulid morphs
});
Auto generate ulid column in your model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Vanthao03596\LaravelUlid\GeneratesUlid;
class User extends Model
{
use GeneratesUlid;
}
Default column is ulid
. If you wish to use a custom column name, for example if you want your primary custom_column
column to be a ULID
, you can define a ulidColumn
method in your model.
class User extends Model
{
public function ulidColumn(): string
{
return 'custom_column';
}
}
You can have multiple ULID columns in each table by specifying an array in the ulidColumns
method.
class User extends Model
{
public function ulidColumns(): array
{
return ['ulid', 'custom_column'];
}
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.