|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder\Searches; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
|
|
9
|
|
|
abstract class SearchesBase implements Search |
|
10
|
|
|
{ |
|
11
|
|
|
protected $relationConstraints = []; |
|
12
|
|
|
|
|
13
|
|
|
abstract public function __invoke(Builder $query, $value, string $property, ?string $modifier = null) : Builder; |
|
14
|
|
|
|
|
15
|
|
|
protected function isRelationProperty(Builder $query, string $property) : bool |
|
16
|
|
|
{ |
|
17
|
|
|
if (! Str::contains($property, '.')) { |
|
18
|
|
|
return false; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
if (in_array($property, $this->relationConstraints)) { |
|
22
|
|
|
return false; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
if (Str::startsWith($property, $query->getModel()->getTable().'.')) { |
|
|
|
|
|
|
26
|
|
|
return false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return true; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function withRelationConstraint(Builder $query, $value, string $property) : Builder |
|
33
|
|
|
{ |
|
34
|
|
|
[$relation, $property] = collect(explode('.', $property)) |
|
|
|
|
|
|
35
|
|
|
->pipe(function (Collection $parts) { |
|
36
|
|
|
return [ |
|
37
|
|
|
$parts->except(count($parts) - 1)->map([Str::class, 'camel'])->implode('.'), |
|
38
|
|
|
$parts->last(), |
|
39
|
|
|
]; |
|
40
|
|
|
}); |
|
41
|
|
|
|
|
42
|
|
|
return $query->whereHas($relation, function (Builder $query) use ($value, $relation, $property) { |
|
43
|
|
|
$this->relationConstraints[] = $property = $query->getModel()->getTable().'.'.$property; |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$this->__invoke($query, $value, $property); |
|
46
|
|
|
}); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function encloseValue($value) |
|
50
|
|
|
{ |
|
51
|
|
|
return "{$value}"; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: