Determine Toggle label by state
Is there a way to change the label of a
\Filament\Forms\Components\Toggle
based on its value?
This is what I tried, but it doesn't work:
Toggle::make('active')
->label(fn ($record) => $record->active ? 'Active' : 'Inactive')
12 Replies
What's not working? You tried with two records with different state?
Sorry, I mean I want the label to change when the user toggles the Toggle input
Something like this:
fn ($get)
and not the record.Closure Customization
for more infos
You can probably use fn ($state)
as wellsince it's referencing itself, I'd go with $state as Dennis recommends
Thanks! Now it works:
Toggle::make('active')
->label(fn ($state) => $state ? 'Active' : 'Inactive')
->reactive(),
hello, how can i achieve this but with a filter
dd() the state to see what it actually is. It could be on/off 1/0 or true/false.
It depends on how you model is structured.
thanks, i solved it in another way
I leave this here, in case it helps someone, this was what was translated to do a while ago with the table filter:
I was searching for how to change helperText() return string based on the states of the toggle. It accepts a closure; so the way worked for me was:
toggle::make(is_visible)->label('Visibility)->helperText(fn ($state): string => $state? 'Visible' : 'Hidden')->reactive(),
You should not forget to use the $state and the reactive() method in order to be changing after state update.