fix:edit logs error
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
class ActivityLog
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\ActivityLog;
|
||||
|
||||
trait LogsActivity
|
||||
{
|
||||
protected static function bootLogsActivity(): void
|
||||
{
|
||||
static::created(function ($model) {
|
||||
$model->writeActivityLog('created', $model->getAttributes());
|
||||
});
|
||||
|
||||
static::updated(function ($model) {
|
||||
|
||||
$changes = $model->getChanges();
|
||||
|
||||
unset($changes['updated_at']);
|
||||
|
||||
if (empty($changes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$diff = [];
|
||||
|
||||
foreach ($changes as $key => $newValue) {
|
||||
$diff[$key] = [
|
||||
'old' => $model->getOriginal($key),
|
||||
'new' => $newValue,
|
||||
];
|
||||
}
|
||||
|
||||
$model->writeActivityLog('updated', $diff);
|
||||
});
|
||||
}
|
||||
|
||||
protected function writeActivityLog(string $action, array $data): void
|
||||
{
|
||||
ActivityLog::create([
|
||||
'admin_id' => auth('admin')->id(),
|
||||
'model_type' => static::class,
|
||||
'model_id' => $this->getKey(),
|
||||
'action' => $action,
|
||||
'changes' => $data,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user