Duplicate queries using bulk action

I'm getting a whole bunch of duplicate queries trying to use a bulkAction in my admin panel. I followed pretty much the docs as described here https://filamentphp.com/docs/2.x/tables/actions#custom-forms:
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
BulkAction::make('changeDepartment')
->action(function (Collection $records, array $data): void {
$records->each(function (AccountEntry $entry) use ($data) {
$entry->department = $data['department'];
$entry->save();
});
})
->form(
[
Select::make('department')
->label('Department')
->options(Department::pluck('name'))
->required(),
],
),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
BulkAction::make('changeDepartment')
->action(function (Collection $records, array $data): void {
$records->each(function (AccountEntry $entry) use ($data) {
$entry->department = $data['department'];
$entry->save();
});
})
->form(
[
Select::make('department')
->label('Department')
->options(Department::pluck('name'))
->required(),
],
),
])
Attached is a screenshot of my debugbar when I open this resource on my admin panel. If I use a closure for form() I can defer the loading of departments until the bulk action is triggered, but it still does a bunch of duplicate queries. Is this normal?
2 Replies
Dan Harrin
Dan Harrin2y ago
what is on line 84 of your resource is it a Select field options()? maybe wrap those in fn () =>
36864
36864OP2y ago
Sorry for late reply, had to switch to another project for a while. Wrapping the options paramter in fn() => did the trick. I'll remember to do that in the future. There are still duplicate queries for just fetching the selected entries though.
select * from `account_entries` where `account_entries`.`id` in ('218059', '218058', '218055') and `account_entries`.`deleted_at` is null order by `entry_date` desc
select * from `account_entries` where `account_entries`.`id` in ('218059', '218058', '218055') and `account_entries`.`deleted_at` is null order by `entry_date` desc
This one query is repeated 5 times when opening the bulk action form. Culprit seems to be Conerns/CanSelectRecords. Is there any way this can be cached for the request?

Did you find this page helpful?