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
I think
->when()
is always on the object itself and does not support injection of the record. What are you trying to do?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.
Don't really understand what's the issue with
->color()
etc. sorryMy 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
The ugly workaround is to define everything in the column instead of enum, like the image below.
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?That's clever, I can fall back to the enums functions if it is not supposed to be overwritten by something else.
Solution
That is good enough for me. Thanks a bunch Dennis - much better.