F
Filament20h 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 Koch20h 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
emilOP20h 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 Koch20h ago
Don't really understand what's the issue with ->color() etc. sorry
emil
emilOP20h 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
emilOP20h ago
No description
emil
emilOP20h ago
No description
emil
emilOP20h ago
The ugly workaround is to define everything in the column instead of enum, like the image below.
emil
emilOP20h ago
No description
Dennis Koch
Dennis Koch20h 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
emilOP20h 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
emil20h ago
No description
emil
emilOP20h ago
That is good enough for me. Thanks a bunch Dennis - much better.
Want results from more Discord servers?
Add your server