F
Filament15mo ago
John

Duplicate queries using custom filter

This:
Filter::make('arrangement_id')
->form([
Select::make('arrangement_id')->options(
fn() => Arrangement::pluck('arrangement', 'arrangement_id')
)->label(__('model.Arrangement.1'))
])
Filter::make('arrangement_id')
->form([
Select::make('arrangement_id')->options(
fn() => Arrangement::pluck('arrangement', 'arrangement_id')
)->label(__('model.Arrangement.1'))
])
produces 1 query:
select `arrangement`, `arrangement_id` from `ctlv__arrangement`
select `arrangement`, `arrangement_id` from `ctlv__arrangement`
But if I change the closure to a simple pluck:
/*fn() => */Arrangement::pluck('arrangement', 'arrangement_id')
/*fn() => */Arrangement::pluck('arrangement', 'arrangement_id')
I get duplicate queries. Before the query above, the exact same query is executed 18 times. I cannot relate the number 18 to anything. It's not the number of select options, or records in the result, or anything else I could think of. If I do the same /* fn() => */ on another filter, the same thing happens and I end up with 36 duplicate queries. Backtrace of the single query:
16. /app/Filament/Resources/RequestResource.php:69
17. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:88
18. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:59
19. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:400
21. /vendor/spatie/invade/src/Invader.php:50
16. /app/Filament/Resources/RequestResource.php:69
17. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:88
18. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:59
19. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:400
21. /vendor/spatie/invade/src/Invader.php:50
Backtrace of the duplicated query:
16. /app/Filament/Resources/RequestResource.php:69
17. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:88
18. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:59
19. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:390
20. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:395
16. /app/Filament/Resources/RequestResource.php:69
17. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:88
18. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:59
19. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:390
20. /vendor/filament/filament/src/Resources/Pages/ListRecords.php:395
protected function getTableReorderColumn(): ?string
{
return $this->getResourceTable()->getReorderColumn(); // <-- this is line 390
}

protected function isTableReorderable(): bool
{
return filled($this->getTableReorderColumn()) && static::getResource()::canReorder(); // <-- this is line 395
}
protected function getTableReorderColumn(): ?string
{
return $this->getResourceTable()->getReorderColumn(); // <-- this is line 390
}

protected function isTableReorderable(): bool
{
return filled($this->getTableReorderColumn()) && static::getResource()::canReorder(); // <-- this is line 395
}
It's not a problem to always use a closure, I'm just trying to understand what's going on and prevent similar situations.
4 Replies
Dennis Koch
Dennis Koch15mo ago
The form schema is currently evaluated in multiple places and therefore your query is run multiple times if you don't wrap it inside a closure
John
John15mo ago
Ok. So I guess it's recommended to always use a closure when a query is involved? That's assuming there is no easy way to identify / prevent the cause of such duplicate queries.
Dennis Koch
Dennis Koch15mo ago
I think this was fixed/recuded in V3. But yeah: Always use a closure.
John
John15mo ago
Ok thanks!