awtechs / laravel-public-ulid
Binary ULID public identifiers for Laravel models
v1.0.1
2026-02-28 10:36 UTC
Requires
- php: ^8.2
- illuminate/support: ^12.0
- symfony/uid: ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.0
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/uid7+
Installation
composer require awtechs/laravel-public-ulid
Quick Start
- Add the schema macro in a migration:
Schema::table('users', function (Blueprint $table) { $table->publicUlid(); });
- 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_idis a 16-byte binary column. - Cast:
public_idis cast to a Base32 ULID string viaBinaryUlidCast. - Auto-generation: on
creating, ifpublic_idis missing, a ULID is set. - Route key:
getRouteKeyName()returnspublic_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_idby design. - ULIDs are stored in binary, but are exposed as Base32 strings in PHP.
License
MIT