From 0d0e59ccdd03cc69e2b0eb86d630566568f907eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Debrauwer?= Date: Wed, 26 Apr 2023 17:53:59 +0200 Subject: [PATCH] [10.x] Add 'makeSearchableUsing' method (to allow eager loading when making specific models searchable) (#732) * Add 'makeSearchableUsing' method * Update Searchable.php * Update types in docblock * Update Searchable.php --------- Co-authored-by: Taylor Otwell --- src/Jobs/MakeSearchable.php | 2 +- src/Searchable.php | 13 ++++++++++++- tests/Feature/SearchableTest.php | 1 + tests/Unit/MakeSearchableTest.php | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Jobs/MakeSearchable.php b/src/Jobs/MakeSearchable.php index 6baaa2cc..f5096572 100644 --- a/src/Jobs/MakeSearchable.php +++ b/src/Jobs/MakeSearchable.php @@ -39,6 +39,6 @@ public function handle() return; } - $this->models->first()->searchableUsing()->update($this->models); + $this->models->first()->makeSearchableUsing($this->models)->first()->searchableUsing()->update($this->models); } } diff --git a/src/Searchable.php b/src/Searchable.php index 372affc4..393626b8 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -60,7 +60,7 @@ public function queueMakeSearchable($models) } if (! config('scout.queue')) { - return $models->first()->searchableUsing()->update($models); + return $models->first()->makeSearchableUsing($models)->first()->searchableUsing()->update($models); } dispatch((new Scout::$makeSearchableJob($models)) @@ -151,6 +151,17 @@ public static function makeAllSearchable($chunk = null) ->searchable($chunk); } + /** + * Modify the collection of models being made searchable. + * + * @param \Illuminate\Support\Collection $models + * @return \Illuminate\Support\Collection + */ + protected function makeSearchableUsing(BaseCollection $models) + { + return $models; + } + /** * Modify the query used to retrieve models when making all of the models searchable. * diff --git a/tests/Feature/SearchableTest.php b/tests/Feature/SearchableTest.php index 6657a1aa..2b89ff10 100644 --- a/tests/Feature/SearchableTest.php +++ b/tests/Feature/SearchableTest.php @@ -20,6 +20,7 @@ public function test_searchable_using_update_is_called_on_collection() { $collection = m::mock(); $collection->shouldReceive('isEmpty')->andReturn(false); + $collection->shouldReceive('first->makeSearchableUsing')->with($collection)->andReturn($collection); $collection->shouldReceive('first->searchableUsing->update')->with($collection); $model = new SearchableModel(); diff --git a/tests/Unit/MakeSearchableTest.php b/tests/Unit/MakeSearchableTest.php index 52975224..a7646181 100644 --- a/tests/Unit/MakeSearchableTest.php +++ b/tests/Unit/MakeSearchableTest.php @@ -20,6 +20,7 @@ public function test_handle_passes_the_collection_to_engine() $model = m::mock(), ])); + $model->shouldReceive('makeSearchableUsing')->with($collection)->andReturn($collection); $model->shouldReceive('searchableUsing->update')->with($collection); $job->handle();