-
Notifications
You must be signed in to change notification settings - Fork 53
/
Loggable.php
57 lines (46 loc) · 1.64 KB
/
Loggable.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace Haruncpi\LaravelUserActivity\Traits;
use Illuminate\Support\Facades\DB;
trait Loggable
{
static protected $logTable = 'logs';
static function logToDb($model, $logType)
{
if (!auth()->check() || $model->excludeLogging || !config('user-activity.activated', true)) return;
if ($logType == 'create') $originalData = json_encode($model);
else {
if (version_compare(app()->version(), '7.0.0', '>='))
$originalData = json_encode($model->getRawOriginal()); // getRawOriginal available from Laravel 7.x
else
$originalData = json_encode($model->getOriginal());
}
$tableName = $model->getTable();
$dateTime = date('Y-m-d H:i:s');
$userId = auth()->user()->id;
DB::table(self::$logTable)->insert([
'user_id' => $userId,
'log_date' => $dateTime,
'table_name' => $tableName,
'log_type' => $logType,
'data' => $originalData
]);
}
public static function bootLoggable()
{
if (config('user-activity.log_events.on_edit', false)) {
self::updated(function ($model) {
self::logToDb($model, 'edit');
});
}
if (config('user-activity.log_events.on_delete', false)) {
self::deleted(function ($model) {
self::logToDb($model, 'delete');
});
}
if (config('user-activity.log_events.on_create', false)) {
self::created(function ($model) {
self::logToDb($model, 'create');
});
}
}
}