Navigation | Overlay |
---|---|
t Navigate files | h Toggle hits |
y Change url to tip of branch | m Toggle misses |
b / v Jump to prev/next hit line | p Toggle partial |
z / x Jump to prev/next missed or partial line | 1..9 Toggle flags |
shift + o Open current page in GitHub | a Toggle all on |
/ or ? Show keyboard shortcuts dialog | c Toggle context lines or commits |
1 |
<?php
|
|
2 |
|
|
3 |
namespace Igaster\ModelEvents\Traits; |
|
4 |
|
|
5 |
|
|
6 |
/**
|
|
7 |
* Add this trait to a Model to enable Log custom events.
|
|
8 |
* It will log current auth user
|
|
9 |
*
|
|
10 |
* We must manually call the logModelEvent('Some Event') method to log an event
|
|
11 |
*/
|
|
12 |
|
|
13 |
use Igaster\ModelEvents\LogModelEvent; |
|
14 |
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
15 |
use Illuminate\Support\Facades\Auth; |
|
16 |
use Illuminate\Support\Facades\Log; |
|
17 |
|
|
18 |
trait LogsModelEvents |
|
19 |
{
|
|
20 |
// List of Laravel model events that should be recorded
|
|
21 |
// public static $logModelEvents = ['created','updated'];
|
|
22 |
|
|
23 | 1 |
public static function bootLogsModelEvents() |
24 |
{
|
|
25 |
|
|
26 | 1 |
if (property_exists(self::class, 'logModelEvents')) { |
27 |
|
|
28 | 1 |
foreach (self::$logModelEvents as $eventName) { |
29 |
|
|
30 |
static::$eventName(function ($model) use ($eventName) { |
|
31 | 1 |
$description = $eventName; |
32 |
|
|
33 | 1 |
if ($eventName == 'updating' || $eventName == 'updated') { |
34 | 1 |
if ($dirty = $model->getDirty()) { |
35 |
|
|
36 | 1 |
$changed = []; |
37 | 1 |
foreach ($dirty as $key => $value) { |
38 | 1 |
if (!self::shouldHideKey($key)) { |
39 | 1 |
if (self::shouldSanitizeKey($key)) { |
40 | 1 |
$changed[] = "'$key': ***"; |
41 |
} else { |
|
42 | 1 |
$changed[] = "'$key': [" . ($model->original[$key] ?? '-') . "]→[$value]"; |
43 |
}
|
|
44 |
}
|
|
45 |
}
|
|
46 |
|
|
47 | 1 |
if ($changed) { |
48 | 1 |
$description .= ':' . implode(', ', $changed); |
49 |
}
|
|
50 |
}
|
|
51 |
}
|
|
52 |
|
|
53 | 1 |
$model->logModelEvent($description); |
54 | 1 |
});
|
55 |
|
|
56 |
}
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 | 1 |
private static function shouldHideKey($key): bool |
61 |
{
|
|
62 | 1 |
return isset(self::$dontLogUpdatedColumns) && in_array($key, self::$dontLogUpdatedColumns); |
63 |
}
|
|
64 |
|
|
65 | 1 |
private static function shouldSanitizeKey($key): bool |
66 |
{
|
|
67 | 1 |
return isset(self::$sanitizeUpdatedColumns) && in_array($key, self::$sanitizeUpdatedColumns); |
68 |
}
|
|
69 |
|
|
70 |
|
|
71 |
// ----------------------------------------------
|
|
72 |
// Relationships
|
|
73 |
// ----------------------------------------------
|
|
74 |
|
|
75 |
/**
|
|
76 |
* @return MorphMany
|
|
77 |
*/
|
|
78 | 1 |
public function modelEvents() |
79 |
{
|
|
80 | 1 |
return $this->morphMany(LogModelEvent::class, 'model'); |
81 |
}
|
|
82 |
|
|
83 |
|
|
84 |
// ----------------------------------------------
|
|
85 |
// Methods
|
|
86 |
// ----------------------------------------------
|
|
87 |
|
|
88 | 1 |
public function logModelEvent($description = '') |
89 |
{
|
|
90 | 1 |
if (!$this->getKey()){ |
91 | 1 |
return null; |
92 |
}
|
|
93 |
|
|
94 | 1 |
Log::info($description); |
95 |
|
|
96 | 1 |
return $this->modelEvents()->create([ |
97 | 1 |
'description' => $description, |
98 | 1 |
'user_id' => Auth::check() ? Auth::user()->id : null, |
99 |
]);
|
|
100 |
}
|
|
101 |
|
|
102 | 1 |
public function getModelEvents($count = null) |
103 |
{
|
|
104 | 1 |
$query = $this->modelEvents()->orderBy('id', 'desc'); |
105 | 1 |
if($count) { |
106 | 1 |
$query->limit($count); |
107 |
}
|
|
108 | 1 |
return $query->get(); |
109 |
}
|
|
110 |
|
|
111 | 1 |
public function getLastModelEvent() |
112 |
{
|
|
113 | 1 |
return $this->modelEvents()->orderBy('id', 'desc')->first(); |
114 |
}
|
|
115 |
|
|
116 | 1 |
public function clearModelEvents() |
117 |
{
|
|
118 | 1 |
return $this->modelEvents()->delete(); |
119 |
}
|
|
120 |
|
|
121 |
}
|
Read our documentation on viewing source code .