Effortless Activity Logging in Laravel with Spatie’s Activitylog

Monitoring user activity is a critical requirement for modern web applications. Whether it’s for security auditing, debugging, or providing transparency to your users, knowing "who did what and when" is essential. In the Laravel ecosystem, there is no better tool for this than Spatie’s Laravel-activitylog.

In this article, we’ll explore how to integrate this powerful package into your project to track model changes and custom activities seamlessly.

Why Use Laravel-activitylog?

While you could manually log events using Laravel’s built-in logging, Spatie’s package offers several advantages:

  1. Automated Model Tracking: Automatically log created, updated, and deleted events.
  2. Attribute Comparison: It can store both the old and new values of a model attribute.
  3. Custom Logs: Log manual events like "User logged in" or "Report exported."
  4. Clean API: A fluent syntax that feels native to Laravel.

Installation & Setup

To get started, install the package via Composer:

composer require spatie/laravel-activitylog

After installation, publish the migration and the configuration file:

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"
php artisan migrate

This creates an activity_log table in your database where all the magic happens.


Automating Model Logging

The most common use case is tracking changes in your Eloquent models. By adding the LogsActivity trait and defining the LogOptions, you can start tracking immediately.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;

class Product extends Model
{
    use LogsActivity;

    protected $fillable = ['name', 'price', 'description'];

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['name', 'price'])
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }
}
  • logOnly: Specify which attributes to track.
  • logOnlyDirty: Only save a log if an attribute actually changed.
  • dontSubmitEmptyLogs: Prevents cluttering the database with logs that contain no changes.

Manual Activity Logging

Sometimes you need to log actions that aren't tied to a specific model event. You can use the activity() helper for this:

activity()
   ->performedOn($someContent)
   ->causedBy($user)
   ->withProperties(['action' => 'import', 'status' => 'success'])
   ->log('User imported a new dataset');

Retrieving Logs

Retrieving logs is as simple as working with any other Eloquent model. You can fetch all activity, or filter by the subject:

use Spatie\Activitylog\Models\Activity;

// Get the latest activity
$lastActivity = Activity::all()->last();

// Accessing the causer (the user who did the action)
$lastActivity->causer; 

// Accessing the subject (the model that was changed)
$lastActivity->subject; 

Conclusion

Spatie’s laravel-activitylog is a "set it and forget it" solution that brings immense value to any Laravel application. It ensures your data remains auditable and provides a clear trail of events without cluttering your controller logic.

If you are building an ERP, a CRM, or any data-sensitive application, this package should be at the top of your dependencies list.


Happy coding! For more Laravel tips, stay tuned to my blog at akhmad.dev.

Copyright © 2025 Akhmad.dev