Set text on the badge according to the value
I have this badge that takes from id_topic the topic number on the forum where comments are open.
If id_topic is 0 then comments are closed, if it contains an id above 0 then comments are open.
I have tried this, but inside the badge it always returns me the id of id_topic and not the words "open" or "close"
Solution:Jump to solution
That's why, you need formatStateUsing not badge:
https://filamentphp.com/docs/2.x/tables/columns/text#custom-formatting...
8 Replies
First thing it should be
topic_id
. Stick to naming conventionI will make this change, but to resolve my error? I don't think it affects for not complying with the convention
You are comparing a string to a number...
->badge(fn ($state): string => (int) $state > 0 ? 'Open' : 'Close')
->color(fn ($state): string => (int) $state > 0 ? 'success' : 'gray')
Try that.
It keeps returning me the topic_id
TextColumn::make('topic_id')
->badge(fn ($state): string => (int) $state > 0 ? 'Open' : 'Close')
->color(fn ($state): string => (int) $state > 0 ? 'success' : 'gray')
->label('Comments'),
Solution
That's why, you need formatStateUsing not badge:
https://filamentphp.com/docs/2.x/tables/columns/text#custom-formatting
Thank you very much @toeknee, I just started yesterday with Filament and I get lost in these details. Thank you very much
No problem, we've all been there.