Global search query is pulling in trashed models

My global search queries don't seem to be applying the soft delete scope. Is this intentional? For example, here is one of the queries. The Part model has the soft delete trait.
select top 50 * from [maintenance].[parts] where ([maintenance].[parts].[name] like '%#40%' or [maintenance].[parts].[number] like '%#40%')
select top 50 * from [maintenance].[parts] where ([maintenance].[parts].[name] like '%#40%' or [maintenance].[parts].[number] like '%#40%')
10 Replies
morty
mortyOP2w ago
After looking at the source, it seems the global search query is just using the regular query which when using soft deletes you have to remove the soft delete scope. I've added withoutTrashed() to all of my resources that are global search enabled. It seems like this shouldn't be the default though imo.
public static function getGlobalSearchEloquentQuery(): Builder
{
return static::getEloquentQuery();
}
public static function getGlobalSearchEloquentQuery(): Builder
{
return static::getEloquentQuery();
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScope(SoftDeletingScope::class);
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScope(SoftDeletingScope::class);
}
Dennis Koch
Dennis Koch2w ago
You should just overwrite the table query via modifyQuery() instead of using getEloquentQuery()
morty
mortyOP2w ago
This would then break the edit page and restore/delete functionality, wouldn't it?
Dennis Koch
Dennis Koch2w ago
Oh yes. You are right. Then you probably need to adjust readd the scope via the getGlobalSearchEloquentQuery. @Dan Harrin Maybe we should add a note to the docs? Or is there another way around this?
Dan Harrin
Dan Harrin2w ago
i guess the resource stub could be updated so when we include the geteloquentquery modification, we also include getglobalsearcheloquentquery to apply the scope
morty
mortyOP2w ago
I can create an issue on the repo if you'd like. I'd submit a PR but I'm not sure what you're thinking. It looks like the ->withGlobalScope() signature is different and it's expecting an $identifier.
Dan Harrin
Dan Harrin7d ago
Yeah sure, please open an issue
morty
mortyOP6d ago
GitHub
Global search query is pulling in trashed models · Issue #16123 ·...
Package filament/filament Package Version v3.3.4 Laravel Version v11.44.2 Livewire Version v3.6.2 PHP Version PHP 8.3.20 Problem description When following the documentation to add soft-deletes to ...
toeknee
toeknee6d ago
Soo @morty do you have the filter added with a default value? If you are doing that it's very possible the scope it being re-applied
morty
mortyOP6d ago
Like a filter not related to the trashed filter? In my actual application, yes I have things like this:
Tables\Filters\SelectFilter::make('status')
->label('Status')
->options(WorkOrderStatus::options())
->multiple()
->default(['Open', 'On hold']),
Tables\Filters\SelectFilter::make('status')
->label('Status')
->options(WorkOrderStatus::options())
->multiple()
->default(['Open', 'On hold']),
I added a reproduction repo though with the bare-minimum functionality to show the issue: https://github.com/ryanmortier/filament-global-search-soft-delete-issue/blob/main/app/Filament/Resources/UserResource.php

Did you find this page helpful?