QA
Senior LevelLaravel

How do you implement immutable compliance Audit Logging for Eloquent model mutations?

❓ Interview Prompt: Explain spatie/laravel-activitylog, immutable audit logging, tracking dirty fields (old vs new), and compliance security.

Compliance frameworks (SOC2, HIPAA, GDPR) require auditing all sensitive data mutations:

Implementation:

Using 'spatie/laravel-activitylog' with the 'LogsActivity' trait logs all model updates automatically:

•
Records old vs new dirty attributes ('$changes->old', '$changes->attributes').
•
Captures authenticated user ID, IP address, and browser user-agent.

Code Implementation Example

php
<?php
namespace AppModels;

use IlluminateDatabaseEloquentModel;
use SpatieActivitylogTraitsLogsActivity;
use SpatieActivitylogLogOptions;

class PatientRecord extends Model
{
    use LogsActivity;

    public function getActivitylogOptions(): LogOptions {
        return LogOptions::defaults()
            ->logOnly(['diagnosis', 'prescription'])
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }
}
💡Configuring immutable audit logging for model updates using Spatie Activitylog.
🎯 Interview Key Takeaway

Use Spatie Activitylog to track old vs new attribute modifications for SOC2 and HIPAA compliance auditing.

#Audit Logging#Spatie#Compliance#Security#Senior
Back to all Laravel Questions→

More Laravel Interview Questions

Beginner LevelLaravel

How does the Laravel Service Container manage Dependency Injection and Bindings?

Beginner LevelLaravel

How does Routing work in Laravel including Named Routes and Route Parameters?

Beginner LevelLaravel

What is Blade Templating and how do custom directives work?