Files
baharrezaei-project09/app/Traits/LogsActivity.php
T
2026-08-22 15:13:20 +03:30

50 lines
1.1 KiB
PHP

<?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(),
]);
}
}