56 lines
1.2 KiB
PHP
56 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class AuditLog extends Model
|
|
{
|
|
// Audit log adalah append-only — tiada updated_at
|
|
const UPDATED_AT = null;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'event',
|
|
'auditable_type',
|
|
'auditable_id',
|
|
'old_values',
|
|
'new_values',
|
|
'description',
|
|
'ip_address',
|
|
'user_agent',
|
|
];
|
|
|
|
protected $casts = [
|
|
'old_values' => 'array',
|
|
'new_values' => 'array',
|
|
];
|
|
|
|
// === Relationships ===
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
// === Scopes ===
|
|
|
|
public function scopeForModel(Builder $query, string $type, int $id): Builder
|
|
{
|
|
return $query->where('auditable_type', $type)
|
|
->where('auditable_id', $id);
|
|
}
|
|
|
|
public function scopeByEvent(Builder $query, string $event): Builder
|
|
{
|
|
return $query->where('event', $event);
|
|
}
|
|
|
|
public function scopeRecent(Builder $query, int $days = 30): Builder
|
|
{
|
|
return $query->where('created_at', '>=', now()->subDays($days));
|
|
}
|
|
}
|