-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuditable.php
36 lines (30 loc) · 994 Bytes
/
Auditable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
namespace App\Traits;
use App\Models\AuditLog;
use Illuminate\Database\Eloquent\Model;
trait Auditable
{
public static function bootAuditable()
{
static::created(function (Model $model) {
self::audit('audit:created', $model);
});
static::updated(function (Model $model) {
self::audit('audit:updated', $model, $model->getChanges());
});
static::deleted(function (Model $model) {
self::audit('audit:deleted', $model);
});
}
protected static function audit($description, $model, $changes = [])
{
AuditLog::create([
'description' => $description,
'subject_id' => $model->id ?? null,
'subject_type' => sprintf('%s#%s', get_class($model), $model->id) ?? null,
'user_id' => auth()->id() ?? null,
'properties' => $changes ?: $model,
'host' => request()->ip() ?? null,
]);
}
}