F
Filament7mo ago
Rome

Format state of select filter

I have a select filter with a relationship: SelectFilter::make('employee')->relationship('employee', 'name'). I need to modify de label of each option to show ID + name instead of the name only. formatStateUsing() is not available here.
Solution:
->getOptionLabelFromRecordUsing(fn (Employee $record) => "{$record->id} {$record->name}")
->getOptionLabelFromRecordUsing(fn (Employee $record) => "{$record->id} {$record->name}")
...
Jump to solution
4 Replies
prowler
prowler7mo ago
SelectFilter::make('employee')
->relationship('employee', 'name')
->preload()
->options(function () {
return Employee::all()->pluck('name', 'id')->mapWithKeys(function ($name, $id) {
return [$id => $id . ' - ' . $name];
});
})
SelectFilter::make('employee')
->relationship('employee', 'name')
->preload()
->options(function () {
return Employee::all()->pluck('name', 'id')->mapWithKeys(function ($name, $id) {
return [$id => $id . ' - ' . $name];
});
})
?
Solution
LeandroFerreira
LeandroFerreira7mo ago
->getOptionLabelFromRecordUsing(fn (Employee $record) => "{$record->id} {$record->name}")
->getOptionLabelFromRecordUsing(fn (Employee $record) => "{$record->id} {$record->name}")
Rome
RomeOP7mo ago
Not working when you have a relationship() method, but thanks! Working! Thanks a lot! Didn't find in the doc, I was searching on "Table Builder" -> "Filters".
Want results from more Discord servers?
Add your server