F
Filament10mo 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
toeknee10mo ago
try:
SelectFilter::make('roles')->relationship('roles', 'name')->label(__('Roles'))->preload()->multiple();
SelectFilter::make('roles')->relationship('roles', 'name')->label(__('Roles'))->preload()->multiple();
Marc
MarcOP10mo 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
toeknee10mo ago
If you remove Multiple() does it remove it?
Marc
MarcOP10mo ago
No
Marc
MarcOP10mo ago
/admins
No description
Marc
MarcOP10mo ago
Click in select filter Roles
No description
toeknee
toeknee10mo 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
MarcOP10mo ago
I don't really know how to do that
toeknee
toeknee10mo 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
MarcOP10mo ago
Do you mean opening an issue on github?
toeknee
toeknee10mo ago
Yes
Marc
MarcOP10mo ago
ok, now i put it I thought it was something else
Marc
MarcOP10mo 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
MarcOP10mo ago
One solution I found, is to not use relationship() and instead use options() and query() to filter the results
toeknee
toeknee10mo ago
Interesting, please do note that in the PR
Marc
MarcOP10mo 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()
Want results from more Discord servers?
Add your server