F
Filament11mo ago
nikus

[ Issue ] Enum Casting on Eloquent ORM

Hello fellas, Can anyone let me know when a Eloquent Model has an enum caster ( It doesnt matter if its spatie package backed or PHP Native ones ) i always get the following error when trying to open forms/pages Property type not supported in Livewire for property: ["bar"] Being "bar" the actual value of the enum. The current workaround i have found was to implement a Wireable to the enum, but to me it feels dirty.
<?php

namespace App\Enum;

use Livewire\Wireable;

enum TypeEnum: string implements Wireable
{
case Foo = 'foo';
case Bar = 'bar';

// ... Wireable methods...
}
<?php

namespace App\Enum;

use Livewire\Wireable;

enum TypeEnum: string implements Wireable
{
case Foo = 'foo';
case Bar = 'bar';

// ... Wireable methods...
}
To me its seems like Livewire / Filament doesnt know how to "transpile" this types directly? Version: 3.0.0-beta.9
11 Replies
Dennis Koch
Dennis Koch11mo ago
Where do you use these enum? Check out HasLabel trait etc.
nikus
nikus11mo ago
Morning @Dennis Koch 🍆 ! When using in a table it works fine, but when i try to open any kind of resource view ( Edit/View ) it throws it. I have checked the HasLabel, and i totally understand it, but while i can control my own enums, i cant control 3rd party packages enums, this would requirement me to extend all the packages enums to implement a HasLabel Trait? :p Thanks ❤️
Dennis Koch
Dennis Koch11mo ago
Not sure if this is solved in Livewire. I think enums aren’t supported yet. You probably found this issue, too? https://github.com/livewire/livewire/discussions/4411
GitHub
Allow enums for component properties · livewire livewire · Discussi...
Hi, I think it would make sense to allow PHP8.1 Enums as the type for component properties. Example: enum Type: string { case TYPE_A = 'a'; case TYPE_B = 'b'; } class MyComponent ex...
Dennis Koch
Dennis Koch11mo ago
What does the form component look that you use?
nikus
nikus11mo ago
Its just a normal field, but ill try to replicate in a clean repo and open a issue if thats the case, and yeah i checked that issue, but seems like its merged, so it should work in theory! Thanks a lot for your time Dennis! Gonna fiddle around computer
Dennis Koch
Dennis Koch11mo ago
A Select field? Can you share some code?
nikus
nikus11mo ago
No, has a TextColumn, since its a "visual" only Enum
Dennis Koch
Dennis Koch11mo ago
I thought it's broken on Edit/View? 🤔
nikus
nikus11mo ago
Indeed, i just have the enum on the table, but it refuses to open the edit/view parts, thats the weird part :p, i have actually removed the whole form part together
Dennis Koch
Dennis Koch11mo ago
Try getting the state yourself:
TextColumn::make('enum')
->getStateUsing(fn ($record) => $record->enum->value)
TextColumn::make('enum')
->getStateUsing(fn ($record) => $record->enum->value)
nikus
nikus11mo ago
Interesting, ill try that, i was only using formatStateUsing