First commit
This commit is contained in:
55
app/Models/AuditLog.php
Normal file
55
app/Models/AuditLog.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user