-
Notifications
You must be signed in to change notification settings - Fork 461
fix scout search with soft deleted resource #2305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 1.x
Are you sure you want to change the base?
fix scout search with soft deleted resource #2305
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes Scout search functionality when working with soft-deleted models by properly applying trashed filters to Scout queries. The fix ensures that when users select "with trashed" or "only trashed" filters in Filament tables, the Scout search respects these filters by calling the appropriate methods (withTrashed() or onlyTrashed()) on the Scout builder.
Key changes:
- Added detection of Filament's
TrashedFilterstate inBaseListRecords.php - Applied
withTrashed()oronlyTrashed()to Scout queries based on filter state - Refactored query building code for improved readability and consistency
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
packages/core/src/LunarServiceProvider.php |
Updated orderBySequence macro to accept iterable instead of array and use blank() helper for better null-safety |
packages/admin/src/Support/Resources/BaseResource.php |
Simplified query building by removing unnecessary variables and consolidating method calls |
packages/admin/src/Support/Pages/BaseListRecords.php |
Added trashed filter detection and applied appropriate Scout methods to handle soft-deleted records |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (filled($state = $trashedFilter?->getState()['value'] ?? null)) { | ||
| $state ? $scoutQuery->withTrashed() : $scoutQuery->onlyTrashed(); |
Copilot
AI
Nov 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ternary logic is inverted. When $state is truthy (typically true), it should call withTrashed(), but when $state is falsy (typically false), it should call onlyTrashed(). However, this doesn't align with typical trashed filter conventions where true means 'with trashed' and false means 'only trashed'. This logic should be verified against the actual TrashedFilter state values to ensure correct behavior.
| if (filled($state = $trashedFilter?->getState()['value'] ?? null)) { | |
| $state ? $scoutQuery->withTrashed() : $scoutQuery->onlyTrashed(); | |
| $state = $trashedFilter?->getState()['value'] ?? null; | |
| if ($state === 'with') { | |
| $scoutQuery->withTrashed(); | |
| } elseif ($state === 'only') { | |
| $scoutQuery->onlyTrashed(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bot is wrong, just like previous coderabbit review
| $trashedFilter = collect($this->getTable()->getFilters()) | ||
| ->firstWhere(fn ($filter) => $filter instanceof \Filament\Tables\Filters\TrashedFilter); |
Copilot
AI
Nov 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TrashedFilter class should be imported at the top of the file rather than using the fully qualified class name inline. This improves code readability and follows PHP best practices.
To use Scout search with soft delete model, as per laravel doc
and lunar also mentioned this in the doc
however this doesnt work when searching in the panel with filament table. when set to true, the scout search will not return the records, regardless the table filter already selected "with trashed" or "only trashed"
this PR attemp to fix this
p/s: this is just a quick fix, it might have a better way to detect is the with/only trashed filter is applied before using->with/onlyTrashedon the scout builder, to get a better search result.