F
Filament2mo ago
emil

Injecting $record and using ->when

$record becomes an instance of TextColumn instead of Model when using the ->when condition. Is this expected behaviour? TextColumn::make('state') ->when(fn($record) => dd($record))
12 Replies
Dennis Koch
Dennis Koch2mo ago
I think ->when() is always on the object itself and does not support injection of the record. What are you trying to do?
emil
emilOP2mo ago
Apply certain styling (say color) based on the $record and using Enum at the same time. I know I can inject $record in ->color - however the state column is an Enum and has the colors defined there. Just want to override it based on record conditions As there's no way to get the "parent" record from within the Enum (I assume), using when and then overriding Enum values is the only thing I can think of.
Dennis Koch
Dennis Koch2mo ago
Don't really understand what's the issue with ->color() etc. sorry
emil
emilOP2mo ago
My bad - I probably explain poorly. Perhaps images should help? But if when does not support injection and this is intended behaviour it is what it is
emil
emilOP2mo ago
No description
emil
emilOP2mo ago
No description
emil
emilOP2mo ago
The ugly workaround is to define everything in the column instead of enum, like the image below.
emil
emilOP2mo ago
No description
Dennis Koch
Dennis Koch2mo ago
Why don't you use ->color(fn ($state) => $state->getColor()) etc. if it's on the enum? I mean Label and color should be supported already. So it's just the tooltip?
emil
emilOP2mo ago
That's clever, I can fall back to the enums functions if it is not supposed to be overwritten by something else.
Solution
emil
emil2mo ago
No description
emil
emilOP2mo ago
That is good enough for me. Thanks a bunch Dennis - much better.
Want results from more Discord servers?
Add your server