F
Filament4mo ago
Marc

Unnecessary SQL in select filter using relations

I am using SelectFilter with preload, when the page loads the SQL is being executed, but the select does not show any data, so, every time you click on the select, it makes another call to the server and performs another SQL query, the same as the previous one. Is there any way to avoid it?
SelectFilter::make('roles')->label(__('Roles'))->preload()->multiple()->relationship('roles', 'name');
SelectFilter::make('roles')->label(__('Roles'))->preload()->multiple()->relationship('roles', 'name');
select distinct `roles`.* from `roles` left join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` order by `roles`.`name` asc
select distinct `roles`.* from `roles` left join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` order by `roles`.`name` asc
16 Replies
toeknee
toeknee4mo ago
try:
SelectFilter::make('roles')->relationship('roles', 'name')->label(__('Roles'))->preload()->multiple();
SelectFilter::make('roles')->relationship('roles', 'name')->label(__('Roles'))->preload()->multiple();
Marc
Marc4mo ago
It is the same, when I access the index, where the table and the filtering are, the SQL is executed, and when I open the filtering and click in select Roles, the SQL is executed again
toeknee
toeknee4mo ago
If you remove Multiple() does it remove it?
Marc
Marc4mo ago
No
Marc
Marc4mo ago
/admins
No description
Marc
Marc4mo ago
Click in select filter Roles
No description
toeknee
toeknee4mo ago
It must be skipping the preload check, so preload is working but then on click it's reloading. Can you submit a bug with a reproduction repo?
Marc
Marc4mo ago
I don't really know how to do that
toeknee
toeknee4mo ago
Ok, I wouldn't really worry about it too much as it stands it's a single query. But if I get time I'll look into it
Marc
Marc4mo ago
Do you mean opening an issue on github?
toeknee
toeknee4mo ago
Yes
Marc
Marc4mo ago
ok, now i put it I thought it was something else
Marc
Marc4mo ago
GitHub
SelectFilter->preload it skips the preload check · Issue #11421 · f...
Package filament/filament Package Version 3.2.34 Laravel Version 10.44.0 Livewire Version 3.4.4 PHP Version 8.3.1 Problem description I am using SelectFilter with preload, when the page loads the S...
Marc
Marc4mo ago
One solution I found, is to not use relationship() and instead use options() and query() to filter the results
toeknee
toeknee4mo ago
Interesting, please do note that in the PR
Marc
Marc4mo ago
Ideally, relationship() would have to take care of everything, but since that's not working well, what I'm using now is options() to get the data from the database and query to filter the results from the table . When using options(), there is no need to use preload()