1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
trait Query |
6
|
|
|
{ |
7
|
|
|
// ---------------- |
8
|
|
|
// ADVANCED QUERIES |
9
|
|
|
// ---------------- |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Add another clause to the query (for ex, a WHERE clause). |
13
|
|
|
* |
14
|
|
|
* Examples: |
15
|
|
|
* // $this->crud->addClause('active'); |
16
|
|
|
* $this->crud->addClause('type', 'car'); |
17
|
|
|
* $this->crud->addClause('where', 'name', '==', 'car'); |
18
|
|
|
* $this->crud->addClause('whereName', 'car'); |
19
|
|
|
* $this->crud->addClause('whereHas', 'posts', function($query) { |
20
|
|
|
* $query->activePosts(); |
21
|
|
|
* }); |
22
|
|
|
* |
23
|
|
|
* @param [type] |
24
|
|
|
*/ |
25
|
|
|
public function addClause($function) |
26
|
|
|
{ |
27
|
|
|
return call_user_func_array([$this->query, $function], array_slice(func_get_args(), 1, 3)); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Use eager loading to reduce the number of queries on the table view. |
32
|
|
|
* |
33
|
|
|
* @param [type] |
34
|
|
|
* @param string |
35
|
|
|
* |
36
|
|
|
* @return [type] |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function with($entities) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
return $this->query->with($entities); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Order the results of the query in a certain way. |
45
|
|
|
* |
46
|
|
|
* @param [type] |
47
|
|
|
* @param string |
48
|
|
|
* |
49
|
|
|
* @return [type] |
|
|
|
|
50
|
|
|
*/ |
51
|
|
|
public function orderBy($field, $order = 'asc') |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
return $this->query->orderBy($field, $order); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Group the results of the query in a certain way. |
58
|
|
|
* |
59
|
|
|
* @param [type] |
60
|
|
|
* @return [type] |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function groupBy($field) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
return $this->query->groupBy($field); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Limit the number of results in the query. |
69
|
|
|
* |
70
|
|
|
* @param [number] |
71
|
|
|
* @return [type] |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
public function limit($number) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
return $this->query->limit($number); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Take a certain number of results from the query. |
80
|
|
|
* |
81
|
|
|
* @param [number] |
82
|
|
|
* @return [type] |
|
|
|
|
83
|
|
|
*/ |
84
|
|
|
public function take($number) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
return $this->query->take($number); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Start the result set from a certain number. |
91
|
|
|
* |
92
|
|
|
* @param int $number [description] |
93
|
|
|
* @return [type] [description] |
|
|
|
|
94
|
|
|
*/ |
95
|
|
|
public function skip($number) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
return $this->query->skip($number); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Count the number of results. |
102
|
|
|
*/ |
103
|
|
|
public function count() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
return $this->query->count(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: