feat: implement system-wide activity logging with database persistence, repository patterns, and admin controller access.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Services\ActivityLogger;
|
||||
|
||||
trait LogsActivity
|
||||
{
|
||||
/**
|
||||
* Tên module hiển thị trong log. Override nếu muốn tên khác.
|
||||
*/
|
||||
protected function getLogName(): string
|
||||
{
|
||||
return class_basename(static::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Các field không ghi vào log. Override để thêm field riêng của model.
|
||||
*/
|
||||
protected function getLogHiddenFields(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function bootLogsActivity(): void
|
||||
{
|
||||
static::created(function ($model) {
|
||||
app(ActivityLogger::class)->logCreated($model, $model->getLogName());
|
||||
});
|
||||
|
||||
static::updated(function ($model) {
|
||||
app(ActivityLogger::class)->logUpdated($model, $model->getLogName());
|
||||
});
|
||||
|
||||
static::deleted(function ($model) {
|
||||
app(ActivityLogger::class)->logDeleted($model, $model->getLogName());
|
||||
});
|
||||
|
||||
if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses_recursive(static::class))) {
|
||||
static::restored(function ($model) {
|
||||
app(ActivityLogger::class)->logRestored($model, $model->getLogName());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user