Binary Ulids

Hi I'm trying to build an admin panel for a project. It already has a DB structure with ulid as primary keys. The DB schema has a binary(16) column type. I recreated it this way:
Schema::create('admins', static function (Blueprint $table) {
$table->ulid('id', 16)->charset('binary')->primary();
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->string('avatar')->nullable();
$table->rememberToken();
$table->timestamps();
});
Schema::create('admins', static function (Blueprint $table) {
$table->ulid('id', 16)->charset('binary')->primary();
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->string('avatar')->nullable();
$table->rememberToken();
$table->timestamps();
});
So that it works for the tests (i.e. I have the correct DB structure when running test suite) I also use the HasUlids trait and a custom cast for the primary key (as Laravel expects a char(26) column type):
public function get(Model $model, string $key, mixed $value, array $attributes)
{
if ($value === null) {
return null;
}

return Ulid::fromBinary($value)->toRfc4122();
}

public function set(Model $model, string $key, mixed $value, array $attributes)
{
if ($value === null) {
return null;
}

return Ulid::fromString($value)->toBinary();
}
public function get(Model $model, string $key, mixed $value, array $attributes)
{
if ($value === null) {
return null;
}

return Ulid::fromBinary($value)->toRfc4122();
}

public function set(Model $model, string $key, mixed $value, array $attributes)
{
if ($value === null) {
return null;
}

return Ulid::fromString($value)->toBinary();
}
It seems to work when I dd a model but I can't login to Filament, and tests fail too...
Component has errors: "data.pathway"
Failed asserting that false is true.
at vendor/livewire/livewire/src/Testing/Concerns/MakesAssertions.php:301
at tests/Admin/AdmissionResource/CreateAdmissionTest.php:36
at vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:174
Component has errors: "data.pathway"
Failed asserting that false is true.
at vendor/livewire/livewire/src/Testing/Concerns/MakesAssertions.php:301
at tests/Admin/AdmissionResource/CreateAdmissionTest.php:36
at vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:174
This is one of the errors I can have. The code is available at https://github.com/AlexandreGerault/mei-laravel/tree/refactor/ulid-primary-keys I wonder what I can do to debug this 😅
1 Reply
Gunnolfson
GunnolfsonOP17mo ago
Disabling the cast allows me to log in successfully but I get this error. So the login fails if I enable the cast on ulid. Now that I'm logged in, I can enable the cast again, and it displays correctly the table, but every resource page (edit/detail) fails with a 404 :/ Seems Laravel Eloquent works fine with the ulid charset set as a binary(16) but it breaks the json encode function, which is required to work for Laravel Livewire/Laravel Filament. I'm ** 😭
Want results from more Discord servers?
Add your server