1 | <?php |
||
7 | trait AuditableTrait |
||
8 | { |
||
9 | /** |
||
10 | * Boot the audit trait for a model. |
||
11 | * |
||
12 | * @return void |
||
13 | */ |
||
14 | public static function bootAuditableTrait() |
||
18 | |||
19 | /** |
||
20 | * Get user model who created the record. |
||
21 | * |
||
22 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
23 | */ |
||
24 | public function creator() |
||
28 | |||
29 | /** |
||
30 | * Get column name for created by. |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | public function getCreatedByColumn() |
||
38 | |||
39 | /** |
||
40 | * Get user model who updated the record. |
||
41 | * |
||
42 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
43 | */ |
||
44 | public function updater() |
||
48 | |||
49 | /** |
||
50 | * Get column name for updated by. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function getUpdatedByColumn() |
||
58 | |||
59 | /** |
||
60 | * Get user model who deleted the record. |
||
61 | * |
||
62 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
63 | */ |
||
64 | public function deleter() |
||
68 | |||
69 | /** |
||
70 | * Get column name for deleted by. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getDeletedByColumn() |
||
78 | |||
79 | /** |
||
80 | * Get created by user full name. |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getCreatedByNameAttribute() |
||
92 | |||
93 | /** |
||
94 | * Get Laravel's user class instance. |
||
95 | * |
||
96 | * @return \Illuminate\Database\Eloquent\Model |
||
97 | */ |
||
98 | public function getUserInstance() |
||
104 | |||
105 | /** |
||
106 | * Get updated by user full name. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getUpdatedByNameAttribute() |
||
118 | |||
119 | /** |
||
120 | * Get deleted by user full name. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getDeletedByNameAttribute() |
||
132 | |||
133 | /** |
||
134 | * Query scope to limit results to own records. |
||
135 | * |
||
136 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
137 | * @return \Illuminate\Database\Eloquent\Builder |
||
138 | */ |
||
139 | public function scopeOwned(Builder $query) |
||
143 | |||
144 | /** |
||
145 | * Get qualified column name for user id. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | public function getQualifiedUserIdColumn() |
||
153 | } |
||
154 |
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.