F
Filament16mo ago
Askancy

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"
TextColumn::make('id_topic')
->badge(fn ($state): string => $state > 0 ? 'Open' : 'Close')
->color(fn ($state): string => $state > 0 ? 'success' : 'gray')
->label('Comments'),
TextColumn::make('id_topic')
->badge(fn ($state): string => $state > 0 ? 'Open' : 'Close')
->color(fn ($state): string => $state > 0 ? 'success' : 'gray')
->label('Comments'),
8 Replies
krekas
krekas16mo ago
First thing it should be topic_id. Stick to naming convention
Askancy
AskancyOP16mo ago
I will make this change, but to resolve my error? I don't think it affects for not complying with the convention
toeknee
toeknee16mo ago
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.
Askancy
AskancyOP16mo ago
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'),
toeknee
toeknee16mo ago
TextColumn::make('topic_id')
->badge()
->formatStateUsing(fn ($state): string => (int) $state > 0 ? 'Open' : 'Close')
->color(fn ($state): string => (int) $state > 0 ? 'success' : 'gray')
->label('Comments'),
TextColumn::make('topic_id')
->badge()
->formatStateUsing(fn ($state): string => (int) $state > 0 ? 'Open' : 'Close')
->color(fn ($state): string => (int) $state > 0 ? 'success' : 'gray')
->label('Comments'),
Askancy
AskancyOP16mo ago
Thank you very much @toeknee, I just started yesterday with Filament and I get lost in these details. Thank you very much
toeknee
toeknee16mo ago
No problem, we've all been there.
Want results from more Discord servers?
Add your server