Tracking Model Changes in Laravel with Audit Logs (Made Easy)

Tracking Model Changes in Laravel with Audit Logs (Made Easy)

Ever made a change to a database record and thought, “Wait, who did this?” or “What was the previous value?”

If you’ve worked on any app that handles sensitive data, audit trails are a must. They give you visibility into what’s happening with your models from who made a change, to what was changed, and when it happened.

In this article, I’ll show you how to implement audit logging in Laravel the easy way, using the popular Laravel Auditing package.


Why Use Audit Logs?

Audit logs help you:

  • ✅ Monitor data changes at the model level
  • ✅ Track who made each change (user identification)
  • ✅ Log the before and after values of fields
  • ✅ Maintain accountability in apps with multiple users
  • ✅ Stay compliant for apps handling financial or personal data

Whether you’re building an internal dashboard or a public SaaS, this kind of logging is crucial for debugging, security, and trust.


Step 1: Install Laravel Auditing Package

The most robust and widely used package is owen-it/laravel-auditing. It’s open source and has been around for years.

Install it via Composer:

composer require owen-it/laravel-auditing

Then publish the config file (optional):

php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider"

This will create config/audit.php the heart of how auditing works in your app.


Step 2: Prepare Your Model for Auditing

Let’s say you want to track changes on the User model.

Just implement the Auditable interface and use the Auditable trait:

use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
use OwenIt\Auditing\Auditable;

class User extends Authenticatable implements AuditableContract
{
    use Auditable;

    // your existing model code
}

And… done! Now every time a User record is created, updated, or deleted it will be automatically logged.


Step 3: Understand What Gets Logged

By default, the package stores:

  • Model name (e.g. App\Models\User)
  • Event (created, updated, or deleted)
  • Changed attributes
  • The user_id of the user who made the change (if available)
  • The timestamp

This is stored in the audits table (you can change the name if you want).

To create the table:

php artisan vendor:publish --tag="auditing-migrations"
php artisan migrate

Step 4: Viewing the Logs

Once your app is live and users start making changes, entries will appear in the audits table.

Want to show audit logs in your UI?

You can fetch them like this:

$user = User::find(1);
$audits = $user->audits;

Loop through and display them however you want:

@foreach($audits as $audit)
    <p>
        Event: {{ $audit->event }}<br>
        By: {{ optional($audit->user)->name ?? 'System' }}<br>
        Changed: {{ json_encode($audit->getModified()) }}
    </p>
@endforeach

Step 5: Customizing Audit Behavior

You might not want to log every change especially for internal fields or timestamps.

Inside your model, override the getAuditExclude() or getAuditInclude() methods:

public function getAuditExclude()
{
    return ['password', 'remember_token'];
}

Or if you want to only log specific fields:

public function getAuditInclude()
{
    return ['email', 'name', 'role'];
}

You can also customize how the user is resolved or modify the stored data via event hooks.


Step 6: Audit Other Events (Optional)

By default, Laravel Auditing hooks into the Eloquent created, updated, and deleted events.

Want to log login activity, API requests, or other custom events? You can create manual audits:

use OwenIt\Auditing\Models\Audit;

Audit::create([
    'user_type' => 'App\Models\User',
    'user_id' => auth()->id(),
    'event' => 'logged_in',
    'auditable_type' => null,
    'auditable_id' => null,
    'old_values' => [],
    'new_values' => [],
]);

Not necessary for most CRUD apps, but great for apps that require additional audit layers.


Example Use Cases

  • Admin updates a user’s role → audit logs track the old and new role
  • A user deletes a blog post → you see when and what was deleted
  • An order’s status changes → you log the transition from pending to shipped

These records give you peace of mind and evidence in the event something goes wrong.


Final Thoughts

Laravel + Audit Logging = transparency by default.

If you’re building any kind of multi-user app where data integrity matters, implementing audit logs is a no-brainer. And with the owen-it/laravel-auditing package, it only takes a few lines of code to make it happen.

No need to manually track changes, write database triggers, or reinvent the wheel.

Just install, tag your models, and relax your app now has a memory.


Pro Tip:
Store audits in a separate database connection or archive periodically if you expect high write volume it can grow fast over time.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top