platform
platform copied to clipboard
Error with enum cast and select field
If model field casted as enum class, e.g.:
protected $casts = [
'user_type' => UserTypeEnum::class,
];
and select field is used to edit it:
Select::make('user_type')->options(
collect(UserTypeEnum::cases())
->mapWithKeys(fn($case) => [$case->value => $case->title()])
),
fatal php error is appearing:
Could you please explain why options is used here when the value isn’t an array?
It seems more logical to use options() for arrays and fromEnum() for enum values.
Oh, forgot about fromEnum method, thanks! But shouldn't code works even with options? Shouldn't that:
Select::make()->value('some str value')->options([
'some str value' => 'some str title',
'some other str value' => 'some other str value',
]),
be equal to:
Select::make()->value(StringEnum::someCase)->options([
StringEnum::someCase->value => 'some str title',
StringEnum::someOtherCase->value => 'some other str value',
]),
and work in both ways?