1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Modules\Core\Traits\Criteria; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait ParserOrderByTrait |
11
|
|
|
* |
12
|
|
|
* @property \Illuminate\Http\Request $request |
13
|
|
|
* @property \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model |
14
|
|
|
* @property $search |
15
|
|
|
* @property $searchData |
16
|
|
|
* @property $searchFields |
17
|
|
|
* @property $isFirstField = true |
18
|
|
|
* @property $modelForceAndWhere |
19
|
|
|
* @property $fieldsSearchable |
20
|
|
|
* @property $fields |
21
|
|
|
* @property $filter |
22
|
|
|
* @property $orderBy |
23
|
|
|
* @property $sortedBy |
24
|
|
|
* @property $with |
25
|
|
|
* @property $searchJoin |
26
|
|
|
* @property $acceptedConditions |
27
|
|
|
* @property $originalFields |
28
|
|
|
* @package Modules\Core\Traits\Criteria |
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
trait ParserOrderByTrait |
31
|
|
|
{ |
32
|
|
|
protected function parserOrderBy() |
33
|
|
|
{ |
34
|
|
|
$this->orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null); |
|
|
|
|
35
|
|
|
$this->sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc'); |
|
|
|
|
36
|
|
|
$this->sortedBy = !empty($this->sortedBy) ? $this->sortedBy : 'asc'; |
37
|
|
|
|
38
|
|
|
if (isset($this->orderBy) && !empty($this->orderBy)) { |
39
|
|
|
|
40
|
|
|
$split = explode('|', $this->orderBy); |
41
|
|
|
|
42
|
|
|
if (count($split) > 1) { |
43
|
|
|
$this->multiOrderBy($split); |
44
|
|
|
} else { |
45
|
|
|
$this->orderBy(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function orderBy() |
51
|
|
|
{ |
52
|
|
|
$this->model = $this->model->orderBy($this->orderBy, $this->sortedBy); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function multiOrderBy($split) |
56
|
|
|
{ |
57
|
|
|
$table = $this->model->getModel()->getTable(); |
58
|
|
|
$sortTable = $split[0]; |
59
|
|
|
$sortColumn = $split[1]; |
60
|
|
|
|
61
|
|
|
$split = explode(':', $sortTable); |
62
|
|
|
|
63
|
|
|
if (count($split) > 1) { |
64
|
|
|
$sortTable = $split[0]; |
65
|
|
|
$keyName = $table.'.'.$split[1]; |
66
|
|
|
} else { |
67
|
|
|
$prefix = Str::singular($sortTable); |
68
|
|
|
$keyName = $table.'.'.$prefix.'_id'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->model = $this->model->leftJoin($sortTable, $keyName, '=', $sortTable.'.id') |
|
|
|
|
72
|
|
|
->orderBy($sortColumn, $this->sortedBy) |
73
|
|
|
->addSelect($table.'.*'); |
74
|
|
|
} |
75
|
|
|
} |