From 228bfca19949fcfd231f87d748c7ce2ec5d6776f Mon Sep 17 00:00:00 2001 From: Eric Jensen Date: Sun, 29 Oct 2023 12:21:08 -0500 Subject: [PATCH] call makeSearchableUsing before searching --- src/Engines/CollectionEngine.php | 2 +- tests/Feature/CollectionEngineTest.php | 11 ++++++++ .../SearchableModelWithUnloadedValue.php | 28 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/SearchableModelWithUnloadedValue.php diff --git a/src/Engines/CollectionEngine.php b/src/Engines/CollectionEngine.php index eadd398f..a759e6c9 100644 --- a/src/Engines/CollectionEngine.php +++ b/src/Engines/CollectionEngine.php @@ -121,7 +121,7 @@ protected function searchModels(Builder $builder) return $models; } - return $models->filter(function ($model) use ($builder) { + return $models->first()->makeSearchableUsing($models)->filter(function ($model) use ($builder) { if (! $model->shouldBeSearchable()) { return false; } diff --git a/tests/Feature/CollectionEngineTest.php b/tests/Feature/CollectionEngineTest.php index cb1da738..53ed35ec 100644 --- a/tests/Feature/CollectionEngineTest.php +++ b/tests/Feature/CollectionEngineTest.php @@ -2,7 +2,9 @@ namespace Laravel\Scout\Tests\Feature; +use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; +use Laravel\Scout\Tests\Fixtures\SearchableModelWithUnloadedValue; use Laravel\Scout\Tests\Fixtures\SearchableUserModel; use Laravel\Scout\Tests\Fixtures\SearchableUserModelWithCustomSearchableData; use Orchestra\Testbench\Concerns\WithLaravelMigrations; @@ -139,4 +141,13 @@ public function test_it_can_order_by_latest_and_oldest() $this->assertCount(1, $models); $this->assertEquals('Taylor Otwell', $models[0]->name); } + + public function test_it_calls_make_searchable_using_before_searching() + { + Model::preventAccessingMissingAttributes(true); + + $models = SearchableModelWithUnloadedValue::search('loaded')->get(); + + $this->assertCount(2, $models); + } } diff --git a/tests/Fixtures/SearchableModelWithUnloadedValue.php b/tests/Fixtures/SearchableModelWithUnloadedValue.php new file mode 100644 index 00000000..1526adfe --- /dev/null +++ b/tests/Fixtures/SearchableModelWithUnloadedValue.php @@ -0,0 +1,28 @@ + $this->unloadedValue, + ]; + } + + public function makeSearchableUsing(Collection $models) + { + return $models->each( + fn ($model) => $model->unloadedValue = 'loaded', + ); + } +}