1 | <?php |
||
11 | trait AuditableTrait |
||
12 | { |
||
13 | /** |
||
14 | * Boot the audit trait for a model. |
||
15 | * |
||
16 | * @return void |
||
17 | */ |
||
18 | public static function bootAuditableTrait() |
||
22 | |||
23 | /** |
||
24 | * Get user model who created the record. |
||
25 | * |
||
26 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
27 | */ |
||
28 | public function creator() |
||
32 | |||
33 | /** |
||
34 | * Get user class. |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | protected function getUserClass() |
||
46 | |||
47 | /** |
||
48 | * Get column name for created by. |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function getCreatedByColumn() |
||
56 | |||
57 | /** |
||
58 | * Get user model who updated the record. |
||
59 | * |
||
60 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
61 | */ |
||
62 | public function updater() |
||
66 | |||
67 | /** |
||
68 | * Get column name for updated by. |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getUpdatedByColumn() |
||
76 | |||
77 | /** |
||
78 | * Get created by user full name. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getCreatedByNameAttribute() |
||
86 | |||
87 | /** |
||
88 | * Get updated by user full name. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getUpdatedByNameAttribute() |
||
96 | |||
97 | /** |
||
98 | * Query scope to limit results to own records. |
||
99 | * |
||
100 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
101 | * @return \Illuminate\Database\Eloquent\Builder |
||
102 | */ |
||
103 | public function scopeOwned(Builder $query) |
||
107 | |||
108 | /** |
||
109 | * Get qualified column name for user id. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getQualifiedUserIdColumn() |
||
117 | |||
118 | /** |
||
119 | * Get Laravel's user class instance. |
||
120 | * |
||
121 | * @return \Illuminate\Database\Eloquent\Model |
||
122 | */ |
||
123 | public function getUserInstance() |
||
129 | } |
||
130 |
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.