F
Filament16mo ago
Oumaima

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:
This should work then: ```php SelectFilter::make('role') ->label('Role')...
Jump to solution
19 Replies
Oumaima
OumaimaOP16mo ago
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
Dennis Koch
Dennis Koch16mo ago
What are you struggling with? You need a Select Filter and provide options to filter for.
Oumaima
OumaimaOP16mo ago
I already get the options , but when I click for example on : super_admin , they give me nothing
Dennis Koch
Dennis Koch16mo ago
Then please share some code.
Oumaima
OumaimaOP16mo ago
Tables\Filters\SelectFilter::make('role.name') ->label('Role') ->options(Role::all()->pluck('name' , 'id'))
Dennis Koch
Dennis Koch16mo ago
You probably want to filter on role_id
Tables\Filters\SelectFilter::make('role_id')
->label('Role')
->options(Role::all()->pluck('name' , 'id'))
Tables\Filters\SelectFilter::make('role_id')
->label('Role')
->options(Role::all()->pluck('name' , 'id'))
Oumaima
OumaimaOP16mo ago
but I don't have role_id in my users tables , I do just $user->hasRol() to get the user role
Dennis Koch
Dennis Koch16mo ago
Then it's probably:
Tables\Filters\SelectFilter::make('roles.id')
->label('Role')
->options(Role::all()->pluck('name' , 'id'))
Tables\Filters\SelectFilter::make('roles.id')
->label('Role')
->options(Role::all()->pluck('name' , 'id'))
There's a roles() relationship right?
Oumaima
OumaimaOP16mo ago
public function role(){
return $this->belongsTo(Role::class ); } is this correct ?
Oumaima
OumaimaOP16mo ago
does not work , Oh God !
Dennis Koch
Dennis Koch16mo ago
So it's a belongTo(). Then it's probably:
Tables\Filters\SelectFilter::make('role.id')
->label('Role')
->options(Role::all()->pluck('name' , 'id'))
Tables\Filters\SelectFilter::make('role.id')
->label('Role')
->options(Role::all()->pluck('name' , 'id'))
Oumaima
OumaimaOP16mo ago
I'm going out of my mind , I tried every solution , but it's not working
Dennis Koch
Dennis Koch16mo ago
This is Spatie permissions, right?
Oumaima
OumaimaOP16mo ago
I'm Shield package , which use spatie
Solution
Dennis Koch
Dennis Koch16mo ago
This should work then:
SelectFilter::make('role')
->label('Role')
->relationship('roles', 'name')
SelectFilter::make('role')
->label('Role')
->relationship('roles', 'name')
Oumaima
OumaimaOP16mo ago
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
Oumaima
OumaimaOP16mo ago
thank you , I didn"t know how to use them
Want results from more Discord servers?
Add your server