FilamentF
Filament3y 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'))
    ])

produces 1 query:
select `arrangement`, `arrangement_id` from `ctlv__arrangement`


But if I change the closure to a simple pluck:
    /*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


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


    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.
Was this page helpful?