PHP & Laravel
Eloquent ULID-able
Eases using and generating ulid's in Laravel Eloquent models.
Requirements
- PHP
^8.3 - Laravel
^13.0
Installation
composer require kduma/eloquent-ulidable
Setup
Add the Ulidable trait to your model and create a ulid column in your migration:
use KDuma\Eloquent\Ulidable;
class Post extends Model
{
use Ulidable;
}
$table->ulid()->unique();
Configuration
New style — PHP Attribute (recommended)
use KDuma\Eloquent\Ulidable;
use KDuma\Eloquent\Attributes\HasUlid;
#[HasUlid(field: 'public_id', checkDuplicates: true)]
class Post extends Model
{
use Ulidable;
}
HasUlid parameters: field (default: 'ulid'), checkDuplicates (default: false).
Old style — model properties (deprecated)
class Post extends Model
{
use Ulidable;
protected string $ulid_field = 'public_id'; // ⚠️ deprecated
protected bool $check_for_ulid_duplicates = true; // ⚠️ deprecated
}
Usage
- ULID is auto-generated on
createandupdateif field isnull $model->regenerateUlid()— manually regenerate (save afterwards)Model::whereUlid($ulid)— query scopeModel::byUlid($ulid)— retrieve model by ULID$model->getUlidField()— returns configured column name
Note: This package adds ULID as an additional column alongside the numeric
id, unlike Laravel's built-inHasUlidswhich replaces the primary key.