Skip to content

Commit 102fe09

Browse files
authored
Add newScoutQuery() for Database Engine (#923)
* Add newScoutQuery() for Database Engine * Provide ScoutBuilder for scout query builder * Format
1 parent be296e5 commit 102fe09

File tree

5 files changed

+124
-2
lines changed

5 files changed

+124
-2
lines changed

src/Engines/DatabaseEngine.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,15 @@ protected function buildSearchQuery(Builder $builder)
188188
*/
189189
protected function initializeSearchQuery(Builder $builder, array $columns, array $prefixColumns = [], array $fullTextColumns = [])
190190
{
191+
$query = method_exists($builder->model, 'newScoutQuery')
192+
? $builder->model->newScoutQuery($builder)
193+
: $builder->model->newQuery();
194+
191195
if (blank($builder->query)) {
192-
return $builder->model->newQuery();
196+
return $query;
193197
}
194198

195-
return $builder->model->newQuery()->where(function ($query) use ($builder, $columns, $prefixColumns, $fullTextColumns) {
199+
return $query->where(function ($query) use ($builder, $columns, $prefixColumns, $fullTextColumns) {
196200
$connectionType = $builder->model->getConnection()->getDriverName();
197201

198202
$canSearchPrimaryKey = ctype_digit($builder->query) &&

tests/Feature/DatabaseEngineTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
88
use Orchestra\Testbench\Concerns\WithWorkbench;
99
use Orchestra\Testbench\TestCase;
10+
use Workbench\App\Models\Bookmark;
1011
use Workbench\App\Models\SearchableUser;
12+
use Workbench\Database\Factories\BookmarkFactory;
13+
use Workbench\Database\Factories\ChirpFactory;
1114
use Workbench\Database\Factories\SearchableUserFactory;
1215

1316
class DatabaseEngineTest extends TestCase
@@ -186,4 +189,19 @@ public function test_it_can_order_results()
186189
$this->assertCount(1, $modelsSimplePaginate);
187190
$this->assertEquals('Taylor Otwell', $modelsSimplePaginate[0]->name);
188191
}
192+
193+
public function test_it_uses_scout_query()
194+
{
195+
// create bookmarks with chirp_id
196+
BookmarkFactory::new()
197+
->for(ChirpFactory::new()->create(['content' => 'This chirp is searchable']))
198+
->create([
199+
'label' => 'laravel',
200+
]);
201+
202+
$models = Bookmark::search('chirp')->get();
203+
$this->assertCount(1, $models);
204+
$this->assertEquals('laravel', $models[0]->label);
205+
$this->assertEquals('This chirp is searchable', $models[0]->chirp->content);
206+
}
189207
}

workbench/app/Models/Bookmark.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Workbench\App\Models;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9+
use Laravel\Scout\Searchable;
10+
11+
class Bookmark extends Model
12+
{
13+
use HasFactory;
14+
use Searchable;
15+
16+
public function chirp(): BelongsTo
17+
{
18+
return $this->belongsTo(Chirp::class);
19+
}
20+
21+
public function newScoutQuery(): Builder
22+
{
23+
return $this->newQuery()->join('chirps', 'chirps.id', '=', 'bookmarks.chirp_id');
24+
}
25+
26+
public function toSearchableArray()
27+
{
28+
if (isset($_ENV['bookmark.toSearchableArray'])) {
29+
return value($_ENV['bookmark.toSearchableArray'], $this);
30+
}
31+
32+
return [
33+
'label' => $this->label,
34+
'chirps.content' => '',
35+
];
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Workbench\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Workbench\App\Models\Bookmark;
7+
8+
/**
9+
* @template TModel of \Workbench\App\Models\Bookmark
10+
*
11+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
12+
*/
13+
class BookmarkFactory extends Factory
14+
{
15+
/**
16+
* The name of the factory's corresponding model.
17+
*
18+
* @var class-string<TModel>
19+
*/
20+
protected $model = Bookmark::class;
21+
22+
/**
23+
* Define the model's default state.
24+
*
25+
* @return array<string, mixed>
26+
*/
27+
public function definition(): array
28+
{
29+
return [
30+
'chirp_id' => ChirpFactory::new(),
31+
'label' => fake()->word(),
32+
];
33+
}
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('bookmarks', function (Blueprint $table) {
15+
$table->id();
16+
$table->foreignId('chirp_id');
17+
$table->string('label');
18+
$table->timestamps();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*/
25+
public function down(): void
26+
{
27+
Schema::dropIfExists('bookmarks');
28+
}
29+
};

0 commit comments

Comments
 (0)