awtechs/laravel-public-ulid

Binary ULID public identifiers for Laravel models

Maintainers

Package info

github.com/Lordeagle4/Laravel-Public-Ulid

pkg:composer/awtechs/laravel-public-ulid

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.1 2026-02-28 10:36 UTC

This package is auto-updated.

Last update: 2026-02-28 10:38:56 UTC


README

Binary ULID public identifiers for Laravel models.

This package stores ULIDs in a compact 16-byte binary column, casts them to their canonical Base32 string form, and makes them suitable for public routes.

Requirements

  • PHP 8.2+
  • Laravel 12+
  • symfony/uid 7+

Installation

composer require awtechs/laravel-public-ulid

Quick Start

  1. Add the schema macro in a migration:
Schema::table('users', function (Blueprint $table) {
    $table->publicUlid();
});
  1. Add the trait to your model:
use Awtechs\PublicUlid\Concerns\HasPublicUlid;

final class User extends Model
{
    use HasPublicUlid;
}

Now public_id is automatically created as a ULID, stored as binary, and used for route model binding.

How It Works

  • Binary storage: public_id is a 16-byte binary column.
  • Cast: public_id is cast to a Base32 ULID string via BinaryUlidCast.
  • Auto-generation: on creating, if public_id is missing, a ULID is set.
  • Route key: getRouteKeyName() returns public_id.

Casting

If you need to customize or reuse the cast directly:

use Awtechs\PublicUlid\Casts\BinaryUlidCast;

protected $casts = [
    'public_id' => BinaryUlidCast::class,
];

Validation Rule

use Awtechs\PublicUlid\Rules\ValidUlid;

request()->validate([
    'public_id' => ['required', new ValidUlid()],
]);

Console Command

Decode ULID details from the CLI:

php artisan ulid:inspect 01HZX9J4P7VWH2T0R0X6Q8N5E3

Testing Helpers

use Awtechs\PublicUlid\Testing\AssertsUlids;
use Awtechs\PublicUlid\Testing\GeneratesUlids;

final class ExampleTest extends TestCase
{
    use AssertsUlids;
    use GeneratesUlids;

    public function test_ulid_helpers(): void
    {
        $ulid = $this->newUlid();
        $this->assertValidUlid($ulid);
    }
}

Add the Faker provider if you need ULIDs in factories:

use Awtechs\PublicUlid\Testing\Faker\PublicUlidProvider;

$faker->addProvider(new PublicUlidProvider($faker));
$faker->ulid();

Notes

  • The column name is public_id by design.
  • ULIDs are stored in binary, but are exposed as Base32 strings in PHP.

License

MIT