Skip to content

Commit 0d0e59c

Browse files
[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 <[email protected]>
1 parent f2faf37 commit 0d0e59c

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

src/Jobs/MakeSearchable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public function handle()
3939
return;
4040
}
4141

42-
$this->models->first()->searchableUsing()->update($this->models);
42+
$this->models->first()->makeSearchableUsing($this->models)->first()->searchableUsing()->update($this->models);
4343
}
4444
}

src/Searchable.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function queueMakeSearchable($models)
6060
}
6161

6262
if (! config('scout.queue')) {
63-
return $models->first()->searchableUsing()->update($models);
63+
return $models->first()->makeSearchableUsing($models)->first()->searchableUsing()->update($models);
6464
}
6565

6666
dispatch((new Scout::$makeSearchableJob($models))
@@ -151,6 +151,17 @@ public static function makeAllSearchable($chunk = null)
151151
->searchable($chunk);
152152
}
153153

154+
/**
155+
* Modify the collection of models being made searchable.
156+
*
157+
* @param \Illuminate\Support\Collection $models
158+
* @return \Illuminate\Support\Collection
159+
*/
160+
protected function makeSearchableUsing(BaseCollection $models)
161+
{
162+
return $models;
163+
}
164+
154165
/**
155166
* Modify the query used to retrieve models when making all of the models searchable.
156167
*

tests/Feature/SearchableTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function test_searchable_using_update_is_called_on_collection()
2020
{
2121
$collection = m::mock();
2222
$collection->shouldReceive('isEmpty')->andReturn(false);
23+
$collection->shouldReceive('first->makeSearchableUsing')->with($collection)->andReturn($collection);
2324
$collection->shouldReceive('first->searchableUsing->update')->with($collection);
2425

2526
$model = new SearchableModel();

tests/Unit/MakeSearchableTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function test_handle_passes_the_collection_to_engine()
2020
$model = m::mock(),
2121
]));
2222

23+
$model->shouldReceive('makeSearchableUsing')->with($collection)->andReturn($collection);
2324
$model->shouldReceive('searchableUsing->update')->with($collection);
2425

2526
$job->handle();

0 commit comments

Comments
 (0)