Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Call makeSearchableUsing before searching on CollectionEngine #777

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Engines/CollectionEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/CollectionEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions tests/Fixtures/SearchableModelWithUnloadedValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Laravel\Scout\Tests\Fixtures;

use Illuminate\Foundation\Auth\User as Model;
use Illuminate\Support\Collection;
use Laravel\Scout\Searchable;

class SearchableModelWithUnloadedValue extends Model
{
use Searchable;

protected $table = 'users';

public function toSearchableArray()
{
return [
'value' => $this->unloadedValue,
];
}

public function makeSearchableUsing(Collection $models)
{
return $models->each(
fn ($model) => $model->unloadedValue = 'loaded',
);
}
}