F
Filament15mo ago
Atena.D

How to filter Filament table by nested relationship?

The structure of tables are like this: acc->user->role
now I'm in acc resource and I want to filter the rows based on user roles. How can I do that?
5 Replies
Atena.D
Atena.D15mo ago
I added this. then how should my code be like? I tried this: SelectFilter::make('user.role') ->options(UsersRoles::all()->pluck('role', 'id')) ->label('user role'), but didnt work
LeandroFerreira
LeandroFerreira15mo ago
ahh, ok. You can use:
SelectFilter::make('roles')
->options(UsersRoles::pluck('role', 'id'))
->label('user role')
->query(function (Builder $query, array $data) {
//return $query...
})
SelectFilter::make('roles')
->options(UsersRoles::pluck('role', 'id'))
->label('user role')
->query(function (Builder $query, array $data) {
//return $query...
})
and return the query with the filters... Other example here https://filamentphp.com/docs/2.x/tables/filters#custom-filter-forms
Filament
Filters - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
LeandroFerreira
LeandroFerreira15mo ago
Are you using the Spatie package to manage roles/permissions?
SelectFilter::make('roles')
->relationship('roles', 'name')
SelectFilter::make('roles')
->relationship('roles', 'name')
Atena.D
Atena.D15mo ago
No actually these roles are a little bit different. but I'm now able to do that using whereHas in the query. Thanks!