1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Auditable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @property mixed creator |
9
|
|
|
* @property mixed updater |
10
|
|
|
* @property mixed deleter |
11
|
|
|
*/ |
12
|
|
|
trait AuditableTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Boot the audit trait for a model. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public static function bootAuditableTrait() |
20
|
|
|
{ |
21
|
|
|
static::observe(new AuditableTraitObserver); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get user model who created the record. |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
28
|
|
|
*/ |
29
|
|
|
public function creator() |
30
|
|
|
{ |
31
|
|
|
return $this->belongsTo($this->getUserClass(), $this->getCreatedByColumn())->withDefault(); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get user class. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
protected function getUserClass() |
40
|
|
|
{ |
41
|
|
|
return config('auth.providers.users.model', 'App\User'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get column name for created by. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getCreatedByColumn() |
50
|
|
|
{ |
51
|
|
|
return defined('static::CREATED_BY') ? static::CREATED_BY : 'created_by'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get user model who updated the record. |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
58
|
|
|
*/ |
59
|
|
|
public function updater() |
60
|
|
|
{ |
61
|
|
|
return $this->belongsTo($this->getUserClass(), $this->getUpdatedByColumn())->withDefault(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get Laravel's user class instance. |
66
|
|
|
* |
67
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
68
|
|
|
*/ |
69
|
|
|
public function getUserInstance() |
70
|
|
|
{ |
71
|
|
|
$class = $this->getUserClass(); |
72
|
|
|
|
73
|
|
|
return new $class; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get column name for updated by. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getUpdatedByColumn() |
82
|
|
|
{ |
83
|
|
|
return defined('static::UPDATED_BY') ? static::UPDATED_BY : 'updated_by'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get user model who deleted the record. |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
90
|
|
|
*/ |
91
|
|
|
public function deleter() |
92
|
|
|
{ |
93
|
|
|
return $this->belongsTo($this->getUserClass(), $this->getDeletedByColumn())->withDefault(); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get column name for deleted by. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getDeletedByColumn() |
102
|
|
|
{ |
103
|
|
|
return defined('static::DELETED_BY') ? static::DELETED_BY : 'deleted_by'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get created by user full name. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getCreatedByNameAttribute() |
112
|
|
|
{ |
113
|
|
|
if ($this->{$this->getCreatedByColumn()}) { |
114
|
|
|
return $this->creator->name; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return ''; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get updated by user full name. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getUpdatedByNameAttribute() |
126
|
|
|
{ |
127
|
|
|
if ($this->{$this->getUpdatedByColumn()}) { |
128
|
|
|
return $this->updater->name; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return ''; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get deleted by user full name. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getDeletedByNameAttribute() |
140
|
|
|
{ |
141
|
|
|
if ($this->{$this->getDeletedByColumn()}) { |
142
|
|
|
return $this->deleter->name; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return ''; |
146
|
|
|
} |
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) |
155
|
|
|
{ |
156
|
|
|
return $query->where($this->getQualifiedUserIdColumn(), auth()->id()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get qualified column name for user id. |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getQualifiedUserIdColumn() |
165
|
|
|
{ |
166
|
|
|
return $this->getTable() . '.' . $this->getUserInstance()->getKey(); |
|
|
|
|
167
|
|
|
} |
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.