Pre-select filters in table
Hey guys,
I have widget Staff Widget. Depending on the role which user is logged in, I have to create pre-select filter in the table.
So this is StaffWidget.php in widgets folder :
protected function getTableColumns(): array
{
return [
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('roles.name')
];
}
and this is the code from UserResource.php :
Forms\Components\TextInput::make('name')->required(),
Forms\Components\TextInput::make('email')->required(),
Forms\Components\Select::make('organisation')->relationship('organisation', 'name'),
Bottom line, If I am logged in as a customer, role "customers" should be pre-selected on the Users overview page.
Thanks in advance!10 Replies
where is your filter?
hey @Dan Harrin
Tables\Filters\SelectFilter::make('organisation_id')
->label(__('Organisation'))
->options(function (){
$organisation = User::select('organisation_id')->groupBy('id', 'organisation_id')->get()->toArray();
return Organisation::select()->whereIn('id', $organisation)->get()->pluck('name',
'id')->toArray();
}),
so i need to make it pre-selected with the role "customer" if I'm logged in as a customer @Dan Harrindo you want customers to be able to remove that filter too though?
nope, since they have no permissions for that. For the permissions I'm using Global Scope @Dan Harrin
so why do you need the filter if you are using a scope? you can hide it for those users
I don't actually need it, I need it to be pre-selected.
For example - I have roles : admin, staff, customer.. and from the widget, if I am logged in as a customer, the User table should show only the Customers. @Dan Harrin
but that is handled by your scope isnt it...
if its a customer, scope the query and hide the filter
ok, i will check this and come back to you. thank you very much
so @Dan Harrin
Tables\Filters\SelectFilter::make('role')
->label(__('Role'))
// ->multiple()
->options(function (){
return Role::all()->pluck('name', 'id')->toArray();
})
->relationship('roles')
->default(5),
The above pre selects the role that I want.
However, the role filter should be a multiple select, and I am not sure how to correctly define a default for that?
I should mention that the team lead doesn't want to go down the scopes route.->default([5])
it is. but still for the widgets I decided to use approach in widget itself by adding a route :
route('filament.resources.users.index',['tableFilters[role][values][0]' => 5])
it all helped, thank you very much guys!