1 | <?php |
||
18 | trait CustomOrderTrait |
||
19 | { |
||
20 | /** |
||
21 | * Set the model order |
||
22 | * |
||
23 | * @param Builder $query |
||
24 | * @param string $column |
||
25 | * @param string $direction |
||
26 | * @return Builder |
||
27 | */ |
||
28 | public function scopeSetCustomOrder(Builder $query, $column, $direction) |
||
37 | |||
38 | /** |
||
39 | * Override column if provided column not valid |
||
40 | * |
||
41 | * @param string $column |
||
42 | * @return string |
||
43 | */ |
||
44 | private function setColumn($column) |
||
53 | |||
54 | /** |
||
55 | * Override direction if provided direction not valid |
||
56 | * |
||
57 | * @param string $direction |
||
58 | * @return string |
||
59 | */ |
||
60 | private function setDirection($direction) |
||
69 | |||
70 | /** |
||
71 | * Set order based on order_fields |
||
72 | * |
||
73 | * @param Builder $query |
||
74 | * @param string $column |
||
75 | * @param string $direction |
||
76 | * @return Builder |
||
77 | */ |
||
78 | private function setOrder($query, $column, $direction) |
||
90 | |||
91 | /** |
||
92 | * Check if column being sorted by is from a related model |
||
93 | * |
||
94 | * @param Builder $query |
||
95 | * @param string $column |
||
96 | * @param string $direction |
||
97 | * @return Builder |
||
98 | */ |
||
99 | public function scopeOrderByCheckModel(Builder $query, $column, $direction) |
||
112 | } |
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.