Skip to content

Conversation

@wychoong
Copy link
Contributor

@wychoong wychoong commented Oct 2, 2025

To use Scout search with soft delete model, as per laravel doc

set the soft_delete option of the config/scout.php configuration file to true:

'soft_delete' => true,

When this configuration option is true, Scout will not remove soft deleted models from the search index. Instead, it will set a hidden __soft_deleted attribute on the indexed record. Then, you may use the withTrashed or onlyTrashed methods to retrieve the soft deleted records when searching:

and lunar also mentioned this in the doc

By default, scout has the setting soft_delete set to false. You need to make sure this is set to true otherwise you will see soft deleted models appear in your search results

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/onlyTrashed on the scout builder, to get a better search result.

coderabbitai[bot]

This comment was marked as outdated.

@lunarphp lunarphp deleted a comment from coderabbitai bot Nov 11, 2025
@github-project-automation github-project-automation bot moved this to Todo in Roadmap Nov 11, 2025
@glennjacobs glennjacobs requested a review from Copilot November 12, 2025 09:02
Copy link
Contributor

Copilot AI left a 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 TrashedFilter state in BaseListRecords.php
  • Applied withTrashed() or onlyTrashed() 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.

Comment on lines +40 to +41
if (filled($state = $trashedFilter?->getState()['value'] ?? null)) {
$state ? $scoutQuery->withTrashed() : $scoutQuery->onlyTrashed();
Copy link

Copilot AI Nov 12, 2025

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.

Suggested change
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();

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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

Comment on lines +35 to +36
$trashedFilter = collect($this->getTable()->getFilters())
->firstWhere(fn ($filter) => $filter instanceof \Filament\Tables\Filters\TrashedFilter);
Copy link

Copilot AI Nov 12, 2025

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.

Copilot uses AI. Check for mistakes.
@lunarphp lunarphp deleted a comment from vercel bot Nov 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants