1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
trait Query |
6
|
|
|
{ |
7
|
|
|
public $query; |
8
|
|
|
|
9
|
|
|
// ---------------- |
10
|
|
|
// ADVANCED QUERIES |
11
|
|
|
// ---------------- |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Add another clause to the query (for ex, a WHERE clause). |
15
|
|
|
* |
16
|
|
|
* Examples: |
17
|
|
|
* $this->crud->addClause('active'); |
18
|
|
|
* $this->crud->addClause('type', 'car'); |
19
|
|
|
* $this->crud->addClause('where', 'name', '==', 'car'); |
20
|
|
|
* $this->crud->addClause('whereName', 'car'); |
21
|
|
|
* $this->crud->addClause('whereHas', 'posts', function($query) { |
22
|
|
|
* $query->activePosts(); |
23
|
|
|
* }); |
24
|
|
|
* |
25
|
|
|
* @param callable $function |
26
|
|
|
* |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
public function addClause($function) |
30
|
|
|
{ |
31
|
|
|
return call_user_func_array([$this->query, $function], array_slice(func_get_args(), 1)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Use eager loading to reduce the number of queries on the table view. |
36
|
|
|
* |
37
|
|
|
* @param array|string $entities |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
40
|
|
|
*/ |
41
|
|
|
public function with($entities) |
42
|
|
|
{ |
43
|
|
|
return $this->query->with($entities); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Order the results of the query in a certain way. |
48
|
|
|
* |
49
|
|
|
* @param string $field |
50
|
|
|
* @param string $order |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
53
|
|
|
*/ |
54
|
|
|
public function orderBy($field, $order = 'asc') |
55
|
|
|
{ |
56
|
|
|
if ($this->getRequest()->has('order')) { |
|
|
|
|
57
|
|
|
return $this->query; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->query->orderBy($field, $order); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Order results of the query in a custom way. |
65
|
|
|
* |
66
|
|
|
* @param array $column Column array with all attributes |
67
|
|
|
* @param string $column_direction ASC or DESC |
68
|
|
|
* |
69
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
70
|
|
|
*/ |
71
|
|
|
public function customOrderBy($column, $columnDirection = 'asc') |
72
|
|
|
{ |
73
|
|
|
if (! isset($column['orderLogic'])) { |
74
|
|
|
return $this->query; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->query->getQuery()->orders = null; |
78
|
|
|
|
79
|
|
|
$orderLogic = $column['orderLogic']; |
80
|
|
|
|
81
|
|
|
if (is_callable($orderLogic)) { |
82
|
|
|
return $orderLogic($this->query, $column, $columnDirection); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->query; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Group the results of the query in a certain way. |
90
|
|
|
* |
91
|
|
|
* @param string $field |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
94
|
|
|
*/ |
95
|
|
|
public function groupBy($field) |
96
|
|
|
{ |
97
|
|
|
return $this->query->groupBy($field); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Limit the number of results in the query. |
102
|
|
|
* |
103
|
|
|
* @param int $number |
104
|
|
|
* |
105
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
106
|
|
|
*/ |
107
|
|
|
public function limit($number) |
108
|
|
|
{ |
109
|
|
|
return $this->query->limit($number); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Take a certain number of results from the query. |
114
|
|
|
* |
115
|
|
|
* @param int $number |
116
|
|
|
* |
117
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
118
|
|
|
*/ |
119
|
|
|
public function take($number) |
120
|
|
|
{ |
121
|
|
|
return $this->query->take($number); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Start the result set from a certain number. |
126
|
|
|
* |
127
|
|
|
* @param int $number |
128
|
|
|
* |
129
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
130
|
|
|
*/ |
131
|
|
|
public function skip($number) |
132
|
|
|
{ |
133
|
|
|
return $this->query->skip($number); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Count the number of results. |
138
|
|
|
* |
139
|
|
|
* @return int |
140
|
|
|
*/ |
141
|
|
|
public function count() |
142
|
|
|
{ |
143
|
|
|
return $this->query->count(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function orderByWithPrefix($column_name, $column_direction = 'ASC') |
147
|
|
|
{ |
148
|
|
|
if ($this->driverIsSql()) { |
|
|
|
|
149
|
|
|
return $this->query->orderByRaw($this->model->getTableWithPrefix().'.'.$column_name.' '.$column_direction); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->query->orderBy($column_name, $column_direction); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|