fix:edit logs error

This commit is contained in:
2026-08-22 15:13:20 +03:30
parent df49fa0765
commit f597da9614
5 changed files with 224 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
<?php
namespace App\Traits;
class ActivityLog
{
}
+49
View File
@@ -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(),
]);
}
}