53 lines
1.3 KiB
PHP
53 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
class AuditLog extends Model
|
|
{
|
|
protected $fillable = [
|
|
'user_id',
|
|
'action',
|
|
'description',
|
|
'subject_type',
|
|
'subject_id',
|
|
'ip_address',
|
|
'meta',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'meta' => 'array',
|
|
];
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function subject(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
/** Helper ringkas untuk rekod audit trail tindakan penting */
|
|
public static function record(string $action, ?string $description = null, ?Model $subject = null, array $meta = []): self
|
|
{
|
|
return static::create([
|
|
'user_id' => auth()->id(),
|
|
'action' => $action,
|
|
'description' => $description,
|
|
'subject_type' => $subject ? $subject::class : null,
|
|
'subject_id' => $subject?->getKey(),
|
|
'ip_address' => Request::ip(),
|
|
'meta' => $meta ?: null,
|
|
]);
|
|
}
|
|
}
|