Skip to content

Commit 91b0051

Browse files
authored
call makeSearchableUsing before searching (#777)
1 parent a437210 commit 91b0051

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/Engines/CollectionEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected function searchModels(Builder $builder)
121121
return $models;
122122
}
123123

124-
return $models->filter(function ($model) use ($builder) {
124+
return $models->first()->makeSearchableUsing($models)->filter(function ($model) use ($builder) {
125125
if (! $model->shouldBeSearchable()) {
126126
return false;
127127
}

tests/Feature/CollectionEngineTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Laravel\Scout\Tests\Feature;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
7+
use Laravel\Scout\Tests\Fixtures\SearchableModelWithUnloadedValue;
68
use Laravel\Scout\Tests\Fixtures\SearchableUserModel;
79
use Laravel\Scout\Tests\Fixtures\SearchableUserModelWithCustomSearchableData;
810
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
@@ -139,4 +141,13 @@ public function test_it_can_order_by_latest_and_oldest()
139141
$this->assertCount(1, $models);
140142
$this->assertEquals('Taylor Otwell', $models[0]->name);
141143
}
144+
145+
public function test_it_calls_make_searchable_using_before_searching()
146+
{
147+
Model::preventAccessingMissingAttributes(true);
148+
149+
$models = SearchableModelWithUnloadedValue::search('loaded')->get();
150+
151+
$this->assertCount(2, $models);
152+
}
142153
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Laravel\Scout\Tests\Fixtures;
4+
5+
use Illuminate\Foundation\Auth\User as Model;
6+
use Illuminate\Support\Collection;
7+
use Laravel\Scout\Searchable;
8+
9+
class SearchableModelWithUnloadedValue extends Model
10+
{
11+
use Searchable;
12+
13+
protected $table = 'users';
14+
15+
public function toSearchableArray()
16+
{
17+
return [
18+
'value' => $this->unloadedValue,
19+
];
20+
}
21+
22+
public function makeSearchableUsing(Collection $models)
23+
{
24+
return $models->each(
25+
fn ($model) => $model->unloadedValue = 'loaded',
26+
);
27+
}
28+
}

0 commit comments

Comments
 (0)