Search in a table with role
hey I'm new to filament , thank you for your support guys , my question is : how to make a tables search , or a filter with roles .
Example : get all the users that have a role of customer
Solution:Jump to solution
This should work then:
```php
SelectFilter::make('role')
->label('Role')...
19 Replies
What did you try so far?
Searching tables is easy: https://filamentphp.com/docs/3.x/tables/columns/getting-started#searching
A select filter probably too: https://filamentphp.com/docs/3.x/tables/filters#select-filters
mu purpose is to filter the table of users base on their roles , I'm using Spatie for this , I don't know how , I have already check the links which u give me but no result
What are you struggling with? You need a Select Filter and provide options to filter for.
I already get the options , but when I click for example on : super_admin , they give me nothing
Then please share some code.
Tables\Filters\SelectFilter::make('role.name')
->label('Role')
->options(Role::all()->pluck('name' , 'id'))
You probably want to filter on
role_id
but I don't have role_id in my users tables , I do just $user->hasRol() to get the user role
Then it's probably:
There's a
roles()
relationship right?public function role(){
return $this->belongsTo(Role::class ); } is this correct ?
return $this->belongsTo(Role::class ); } is this correct ?
does not work , Oh God !
So it's a
belongTo()
. Then it's probably:
I'm going out of my mind , I tried every solution , but it's not working
This is Spatie permissions, right?
I'm Shield package , which use spatie
Solution
This should work then:
Oooh God , you saved my life ! thank you so much Dennis ! I appreciate your contribution , I'm new to filament , that"s why I'm facing a tone of problems
Btw. relationship filters are documented here: https://filamentphp.com/docs/3.x/tables/filters#relationship-select-filters
thank you , I didn"t know how to use them