forked from laravel/scout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchableScope.php
66 lines (55 loc) · 2.13 KB
/
SearchableScope.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Laravel\Scout;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Scope;
use Laravel\Scout\Events\ModelsFlushed;
use Laravel\Scout\Events\ModelsImported;
class SearchableScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(EloquentBuilder $builder, Model $model)
{
//
}
/**
* Extend the query builder with the needed functions.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
public function extend(EloquentBuilder $builder)
{
$builder->macro('searchable', function (EloquentBuilder $builder, $chunk = null) {
$builder->chunkById($chunk ?: config('scout.chunk.searchable', 500), function ($models) {
$models->filter->shouldBeSearchable()->searchable();
event(new ModelsImported($models));
});
});
$builder->macro('unsearchable', function (EloquentBuilder $builder, $chunk = null) {
$builder->chunkById($chunk ?: config('scout.chunk.unsearchable', 500), function ($models) {
$models->unsearchable();
event(new ModelsFlushed($models));
});
});
HasManyThrough::macro('searchable', function ($chunk = null) {
$this->chunkById($chunk ?: config('scout.chunk.searchable', 500), function ($models) {
$models->filter->shouldBeSearchable()->searchable();
event(new ModelsImported($models));
});
});
HasManyThrough::macro('unsearchable', function ($chunk = null) {
$this->chunkById($chunk ?: config('scout.chunk.searchable', 500), function ($models) {
$models->unsearchable();
event(new ModelsFlushed($models));
});
});
}
}