|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Modules\Core\Traits\Criteria; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
trait ParseSearchWhereTrait |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var \Illuminate\Http\Request $request */ |
|
11
|
|
|
protected $request; |
|
12
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model */ |
|
13
|
|
|
protected $model; |
|
14
|
|
|
/** @var \Prettus\Repository\Contracts\RepositoryInterface $repository */ |
|
15
|
|
|
protected $repository; |
|
16
|
|
|
protected $search; |
|
17
|
|
|
protected $searchData; |
|
18
|
|
|
protected $searchFields; |
|
19
|
|
|
protected $isFirstField; |
|
20
|
|
|
protected $modelForceAndWhere; |
|
21
|
|
|
protected $fieldsSearchable; |
|
22
|
|
|
protected $fields; |
|
23
|
|
|
protected $filter; |
|
24
|
|
|
protected $orderBy; |
|
25
|
|
|
protected $sortedBy; |
|
26
|
|
|
protected $with; |
|
27
|
|
|
protected $searchJoin; |
|
28
|
|
|
protected $acceptedConditions; |
|
29
|
|
|
protected $originalFields; |
|
30
|
|
|
protected $searchClosures; |
|
31
|
|
|
|
|
32
|
|
|
protected function parseSearchWhere($value, $relation, $field, $condition) |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->isFirstField || $this->modelForceAndWhere) { |
|
35
|
|
|
$this->parseSearchAndWhere($value, $relation, $field, $condition); |
|
36
|
|
|
} else { |
|
37
|
|
|
$this->parseSearchOrWhere($value, $relation, $field, $condition); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function parseSearchOrWhere($value, $relation, $field, $condition) |
|
42
|
|
|
{ |
|
43
|
|
|
if (!is_null($value)) { |
|
44
|
|
|
if (!is_null($relation)) { |
|
45
|
|
|
$this->parseSearchOrRelationClosure($value, $relation, $field, $condition); |
|
46
|
|
|
} else { |
|
47
|
|
|
$this->parseSearchOrClosure($value, $field, $condition); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function parseSearchAndWhere($value, $relation, $field, $condition) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!is_null($value)) { |
|
55
|
|
|
if (!is_null($relation)) { |
|
56
|
|
|
$this->parseSearchAndRelationClosure($value, $relation, $field, $condition); |
|
57
|
|
|
} else { |
|
58
|
|
|
$this->parseSearchAndClosure($value, $field, $condition); |
|
59
|
|
|
} |
|
60
|
|
|
$this->isFirstField = false; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function parseSearchAndRelationClosure($value, $relation, $field, $condition) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->model = $this->model->whereHas($relation, function (Builder $query) use ($condition, $field, $value) { |
|
67
|
|
|
if (is_array($this->searchClosures) && Arr::has($this->searchClosures, $condition)) { |
|
68
|
|
|
$this->searchClosures[$condition]($query, $condition, $field, $value); |
|
69
|
|
|
} else { |
|
70
|
|
|
$query->where($field, $condition, $value); |
|
71
|
|
|
} |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function parseSearchAndClosure($value, $field, $condition) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->model = $this->model->where(function (Builder $query) use ($condition, $field, $value) { |
|
78
|
|
|
if (is_array($this->searchClosures) && Arr::has($this->searchClosures, $condition)) { |
|
79
|
|
|
$this->searchClosures[$condition]($query, $condition, $field, $value); |
|
80
|
|
|
} else { |
|
81
|
|
|
$query->where($field, $condition, $value); |
|
82
|
|
|
} |
|
83
|
|
|
}); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function parseSearchOrClosure($value, $field, $condition) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->model = $this->model->orWhere(function (Builder $query) use ($condition, $field, $value) { |
|
89
|
|
|
if (is_array($this->searchClosures) && Arr::has($this->searchClosures, $condition)) { |
|
90
|
|
|
$this->searchClosures[$condition]($query, $condition, $field, $value); |
|
91
|
|
|
} else { |
|
92
|
|
|
$query->where($field, $condition, $value); |
|
93
|
|
|
} |
|
94
|
|
|
}); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function parseSearchOrRelationClosure($value, $relation, $field, $condition) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->model = $this->model->orWhereHas($relation, function (Builder $query) use ($field, $condition, $value) { |
|
100
|
|
|
if (is_array($this->searchClosures) && Arr::has($this->searchClosures, $condition)) { |
|
101
|
|
|
$this->searchClosures[$condition]($query, $condition, $field, $value); |
|
102
|
|
|
} else { |
|
103
|
|
|
$query->where($field, $condition, $value); |
|
104
|
|
|
} |
|
105
|
|
|
}); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|