F
Filament2y ago
adam

Ability to pre-filter Relation Manager table results

I have a bunch of records with various statuses, I'd like to omit Cancelled when the user loads the page, but allow it as a filtered option. I have a SelectFilter with multiple options, I saw that I could enable default on a toggle or something like that but don't think it works on a select. I also tried to use getTableQuery but the filters are unable to overwrite the results.
3 Replies
awcodes
awcodes2y ago
Did you chain off of the parent::getTableQuery()
ZedoX
ZedoX2y ago
->default() should work on SelectFilter. Please share your code
adam
adamOP2y ago
Sorry, busy week! I have a lesson with a pivot SwimmerLesson that has a relation to Swimmers My Resource is a Lesson and the Relation is a Swimmer. The status is on the SwimmerLesson so the pivot.
protected function getTableQuery(): Builder
{
return parent::getTableQuery()->where('status', 'active');
}
protected function getTableQuery(): Builder
{
return parent::getTableQuery()->where('status', 'active');
}
I have a filter that works when I don't change the table query. But I did add ->default('active)to the filter and that did the trick. Actually, is it possible to have multiple defaults? I have a bunch of statuses, and I mainly want to exclude Cancelled on load. got it
SelectFilter::make('Status')
->multiple()
->options(SwimmerLesson::STATUS)
->default(['active', 'held'])
->query(function (Builder $query, array $data): Builder {

if ($data['values']) {
return $query->whereIn('status', $data['values']);
}

return $query;
}),
SelectFilter::make('Status')
->multiple()
->options(SwimmerLesson::STATUS)
->default(['active', 'held'])
->query(function (Builder $query, array $data): Builder {

if ($data['values']) {
return $query->whereIn('status', $data['values']);
}

return $query;
}),
Want results from more Discord servers?
Add your server