PHP & Laravel

Eloquent ULID-able

Eases using and generating ulid's in Laravel Eloquent models.
GitHub RepositoryReleasesPackagist

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

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 create and update if field is null
  • $model->regenerateUlid() — manually regenerate (save afterwards)
  • Model::whereUlid($ulid) — query scope
  • Model::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-in HasUlids which replaces the primary key.

Copyright © 2026