1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Core\Traits\Criteria; |
4
|
|
|
|
5
|
|
|
trait ParseSearchableTrait |
6
|
|
|
{ |
7
|
|
|
/** @var \Illuminate\Http\Request $request */ |
8
|
|
|
protected $request; |
9
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model */ |
10
|
|
|
protected $model; |
11
|
|
|
/** @var \Prettus\Repository\Contracts\RepositoryInterface $repository */ |
12
|
|
|
protected $repository; |
13
|
|
|
protected $search; |
14
|
|
|
protected $searchData; |
15
|
|
|
protected $searchFields; |
16
|
|
|
protected $isFirstField; |
17
|
|
|
protected $modelForceAndWhere; |
18
|
|
|
protected $fieldsSearchable; |
19
|
|
|
protected $fields; |
20
|
|
|
protected $filter; |
21
|
|
|
protected $orderBy; |
22
|
|
|
protected $sortedBy; |
23
|
|
|
protected $with; |
24
|
|
|
protected $searchJoin; |
25
|
|
|
protected $acceptedConditions; |
26
|
|
|
protected $originalFields; |
27
|
|
|
protected $searchClosures; |
28
|
|
|
|
29
|
|
|
use ParseFieldsSearchTrait; |
30
|
|
|
use ParseSearchWhereTrait; |
31
|
|
|
use ParseSearchDataTrait; |
32
|
|
|
use ParseSearchTrait; |
33
|
|
|
use ParseForceAndWhereTrait; |
34
|
|
|
use ParseValueTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
|
|
protected function parseSearchable() |
40
|
|
|
{ |
41
|
|
|
$this->fieldsSearchable = $this->repository->getFieldsSearchable(); |
42
|
|
|
$this->search = $this->request->get(config('repository.criteria.params.search', 'search'), null); |
43
|
|
|
|
44
|
|
|
if ($this->search && is_array($this->fieldsSearchable) && count($this->fieldsSearchable)) { |
45
|
|
|
$this->parseFieldsSearch(); |
46
|
|
|
$this->parseSearchData(); |
47
|
|
|
$this->parseSearch(); |
48
|
|
|
$this->parseForceAndWhere(); |
49
|
|
|
|
50
|
|
|
foreach ($this->fields as $field => $condition) { |
51
|
|
|
$this->getConditionField($field, $condition); |
52
|
|
|
$value = $this->parseValue($condition, $field); |
53
|
|
|
$relation = null; |
54
|
|
|
$this->getRelationField($field, $relation); |
55
|
|
|
$this->parseSearchWhere($value, $relation, $field, $condition); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getConditionField(&$field, &$condition) |
61
|
|
|
{ |
62
|
|
|
if (is_numeric($field)) { |
63
|
|
|
$field = $condition; |
64
|
|
|
$condition = '='; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function getRelationField(&$field, &$relation) |
69
|
|
|
{ |
70
|
|
|
if (stripos($field, '.')) { |
71
|
|
|
$explode = explode('.', $field); |
72
|
|
|
$field = array_pop($explode); |
73
|
|
|
$relation = implode('.', $explode); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|