1 | <?php |
||
12 | trait AuditableTrait |
||
13 | { |
||
14 | /** |
||
15 | * Boot the audit trait for a model. |
||
16 | * |
||
17 | * @return void |
||
18 | */ |
||
19 | public static function bootAuditableTrait() |
||
23 | |||
24 | /** |
||
25 | * Get user model who created the record. |
||
26 | * |
||
27 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
28 | */ |
||
29 | public function creator() |
||
33 | |||
34 | /** |
||
35 | * Get user class. |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | protected function getUserClass() |
||
43 | |||
44 | /** |
||
45 | * Get column name for created by. |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | public function getCreatedByColumn() |
||
53 | |||
54 | /** |
||
55 | * Get user model who updated the record. |
||
56 | * |
||
57 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
58 | */ |
||
59 | public function updater() |
||
63 | |||
64 | /** |
||
65 | * Get Laravel's user class instance. |
||
66 | * |
||
67 | * @return \Illuminate\Database\Eloquent\Model |
||
68 | */ |
||
69 | public function getUserInstance() |
||
75 | |||
76 | /** |
||
77 | * Get column name for updated by. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getUpdatedByColumn() |
||
85 | |||
86 | /** |
||
87 | * Get user model who deleted the record. |
||
88 | * |
||
89 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
90 | */ |
||
91 | public function deleter() |
||
95 | |||
96 | /** |
||
97 | * Get column name for deleted by. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getDeletedByColumn() |
||
105 | |||
106 | /** |
||
107 | * Get created by user full name. |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getCreatedByNameAttribute() |
||
119 | |||
120 | /** |
||
121 | * Get updated by user full name. |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getUpdatedByNameAttribute() |
||
133 | |||
134 | /** |
||
135 | * Get deleted by user full name. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getDeletedByNameAttribute() |
||
147 | |||
148 | /** |
||
149 | * Query scope to limit results to own records. |
||
150 | * |
||
151 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
152 | * @return \Illuminate\Database\Eloquent\Builder |
||
153 | */ |
||
154 | public function scopeOwned(Builder $query) |
||
158 | |||
159 | /** |
||
160 | * Get qualified column name for user id. |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function getQualifiedUserIdColumn() |
||
168 | } |
||
169 |
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.