awtechs/laravel-public-ulid
最新稳定版本:v1.0.1
Composer 安装命令:
composer require awtechs/laravel-public-ulid
包简介
Binary ULID public identifiers for Laravel models
README 文档
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
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-27