1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Traits\ModelAdmin; |
4
|
|
|
|
5
|
|
|
trait ModelQuerying |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Allows filtering of the query, for instance:. |
9
|
|
|
* |
10
|
|
|
* $query_filter = [ |
11
|
|
|
* 'whereNotNull' => ['parent_id'], |
12
|
|
|
* 'where' => ['name', 'John'], |
13
|
|
|
* ] |
14
|
|
|
* |
15
|
|
|
* Would result in an Eloquent query with the following scope: |
16
|
|
|
* Model::whereNotNull('parent_id')->where('name', 'John')->get(); |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $query_filter = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The number of models to return for pagination. |
24
|
|
|
* |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $perPage = 15; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Order By - Column/Attribute to OrderBy. |
31
|
|
|
* |
32
|
|
|
* Primary Key of Model by default |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $orderBy; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sort By - Either Desc or Asc. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $sortBy; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Finds an existing Model entry and sets it to the current model. |
47
|
|
|
* |
48
|
|
|
* @param int $modelitem_id |
49
|
|
|
* |
50
|
|
|
* @return |
51
|
|
|
*/ |
52
|
|
|
public function find($modelitem_id) |
53
|
|
|
{ |
54
|
|
|
$this->model = $this->model->findOrFail($modelitem_id); |
55
|
|
|
|
56
|
|
|
return $this->model; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns Model Items, either all() or paginated(). |
61
|
|
|
* |
62
|
|
|
* Filtered by any defined query filters ($query_filter) |
63
|
|
|
* Ordered by Managed Model orderBy and sortBy methods |
64
|
|
|
* |
65
|
|
|
* @return |
66
|
|
|
*/ |
67
|
|
|
public function items() |
68
|
|
|
{ |
69
|
|
|
$model = $this->model; |
70
|
|
|
|
71
|
|
|
if (count($this->query_filter) > 0) { |
72
|
|
|
foreach ($this->query_filter as $filter => $parameters) { |
73
|
|
|
if (!is_array($parameters)) { |
74
|
|
|
$parameters = [$parameters]; |
75
|
|
|
} |
76
|
|
|
$model = call_user_func_array([$this->model, $filter], $parameters); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->orderBy()) { |
81
|
|
|
$model = $model->orderBy( |
82
|
|
|
$this->orderBy(), |
83
|
|
|
$this->sortBy() |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($this->perPage > 0) { |
88
|
|
|
return $model->paginate($this->perPage); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $model->all(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function totals() |
95
|
|
|
{ |
96
|
|
|
if ($this->hasSoftDeletes()) { |
|
|
|
|
97
|
|
|
return [ |
98
|
|
|
'all' => $this->model->count(), |
99
|
|
|
'with_trashed' => $this->model->withTrashed()->count(), |
100
|
|
|
'only_trashed' => $this->model->onlyTrashed()->count(), |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return ['all' => $this->model->count()]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return Managed Model OrderBy. |
111
|
|
|
* |
112
|
|
|
* Primary key is default. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function orderBy() |
117
|
|
|
{ |
118
|
|
|
if (\Request::input('order')) { |
119
|
|
|
return \Request::input('order'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($this->orderBy) { |
123
|
|
|
return $this->orderBy; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return Managed Model SortBy (Asc or Desc). |
129
|
|
|
* |
130
|
|
|
* Descending is default. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function sortBy() |
135
|
|
|
{ |
136
|
|
|
if (\Request::input('sort')) { |
137
|
|
|
return \Request::input('sort'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($this->sortBy == 'asc') { |
141
|
|
|
return 'asc'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return 'desc'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the number of models to return per page. |
149
|
|
|
* |
150
|
|
|
* @return int |
151
|
|
|
*/ |
152
|
|
|
public function getPerPage() |
153
|
|
|
{ |
154
|
|
|
return $this->perPage; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set the number of models to return per page. |
159
|
|
|
* |
160
|
|
|
* @param int $perPage |
161
|
|
|
*/ |
162
|
|
|
public function setPerPage($perPage) |
163
|
|
|
{ |
164
|
|
|
$this->perPage = $perPage; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.