Skip to content

Commit

Permalink
[10.x] Add 'makeSearchableUsing' method (to allow eager loading when …
Browse files Browse the repository at this point in the history
…making specific models searchable) (#732)

* Add 'makeSearchableUsing' method

* Update Searchable.php

* Update types in docblock

* Update Searchable.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
gdebrauwer and taylorotwell authored Apr 26, 2023
1 parent f2faf37 commit 0d0e59c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Jobs/MakeSearchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 12 additions & 1 deletion src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/SearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/MakeSearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0d0e59c

Please sign in to comment.