1 | <?php |
||
5 | trait ModelQuerying |
||
6 | { |
||
7 | /** |
||
8 | * Query |
||
9 | * |
||
10 | * @var string |
||
11 | */ |
||
12 | public $query; |
||
13 | |||
14 | /** |
||
15 | * Allows filtering of the default query, for instance:. |
||
16 | * |
||
17 | * $queryFilter = [ |
||
18 | * 'whereNotNull' => ['parent_id'], |
||
19 | * 'where' => ['name', 'John'], |
||
20 | * ] |
||
21 | * |
||
22 | * Would result in an Eloquent query with the following scope: |
||
23 | * Model::whereNotNull('parent_id')->where('name', 'John')->get(); |
||
24 | * |
||
25 | * Note: This queryFilter is not used for custom filters and |
||
26 | * can also be overridden by setQueryFilter(); |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $queryFilter = []; |
||
31 | |||
32 | /** |
||
33 | * The number of models to return for pagination. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $perPage = 15; |
||
38 | |||
39 | /** |
||
40 | * Order By - Column/Attribute to OrderBy. |
||
41 | * |
||
42 | * Primary Key of Model by default |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $orderBy; |
||
47 | |||
48 | /** |
||
49 | * Sort By - Either Desc or Asc. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $sortBy; |
||
54 | |||
55 | /** |
||
56 | * Finds an existing Model entry and sets it to the current model. |
||
57 | * |
||
58 | * @param int $modelitemId |
||
59 | * |
||
60 | * @return |
||
61 | */ |
||
62 | public function find($modelitemId) |
||
68 | |||
69 | /** |
||
70 | * Returns Model Items, either all() or paginated(). |
||
71 | * |
||
72 | * Filtered by any defined query filters ($queryFilter) |
||
73 | * Ordered by Managed Model orderBy and sortBy methods |
||
74 | * |
||
75 | * @return |
||
76 | */ |
||
77 | public function items() |
||
83 | |||
84 | /** |
||
85 | * Returns All Model Items, either all() or paginated(). |
||
86 | * |
||
87 | * Filtered by any defined query filters ($queryFilter) |
||
88 | * Ordered by Managed Model orderBy and sortBy methods |
||
89 | * |
||
90 | * @return |
||
91 | */ |
||
92 | public function allItems() |
||
102 | |||
103 | /** |
||
104 | * Returns Model Items, either all() or paginated(). |
||
105 | * |
||
106 | * Filtered by any defined query filters ($queryFilter) |
||
107 | * Ordered by Managed Model orderBy and sortBy methods |
||
108 | * |
||
109 | * @return |
||
110 | */ |
||
111 | public function onlyTrashedItems() |
||
121 | |||
122 | /** |
||
123 | * Performs the Model Query |
||
124 | * |
||
125 | * @return \Illuminate\Database\Eloquent\Collection |
||
126 | */ |
||
127 | private function query() |
||
144 | |||
145 | /** |
||
146 | * Return Totals of All, With Trashed and Only Trashed |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | public function totals() |
||
162 | |||
163 | /** |
||
164 | * Return Managed Model OrderBy. |
||
165 | * |
||
166 | * Primary key is default. |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function orderBy() |
||
180 | |||
181 | /** |
||
182 | * Return Managed Model SortBy (Asc or Desc). |
||
183 | * |
||
184 | * Descending is default. |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | public function sortBy() |
||
200 | |||
201 | /** |
||
202 | * Get the number of models to return per page. |
||
203 | * |
||
204 | * @return int |
||
205 | */ |
||
206 | public function getPerPage() |
||
210 | |||
211 | /** |
||
212 | * Set the number of models to return per page. |
||
213 | * |
||
214 | * @param int $perPage |
||
215 | */ |
||
216 | public function setPerPage($perPage) |
||
220 | |||
221 | /** |
||
222 | * Apply the Query Filters |
||
223 | * |
||
224 | * @return |
||
225 | */ |
||
226 | private function applyQueryFilters() |
||
234 | |||
235 | /** |
||
236 | * Create the Query Filter from Array |
||
237 | * |
||
238 | * @return |
||
239 | */ |
||
240 | private function createQueryFilter() |
||
251 | |||
252 | /** |
||
253 | * Access the Query Filter |
||
254 | * |
||
255 | * @return |
||
256 | */ |
||
257 | public function getQueryFilter() |
||
261 | |||
262 | /** |
||
263 | * Set the Query Filter |
||
264 | * |
||
265 | * @param array $filter |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function setQueryFilter($filter = []) |
||
273 | } |
||
274 |
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.