Log User Activity in Your Laravel App with Activity Log v5

Last updated on by

Log User Activity in Your Laravel App with Activity Log v5  image

Laravel Activitylog by Spatie is a package for recording what users do in your Laravel application. It stores all activity in an activity_log database table and supports both manual logging and automatic event tracking on Eloquent models.

Recently released, version 5 ships with PHP 8.4 and Laravel 12 as minimum requirements, removes some legacy features, and modernizes the API.

HasActivity Trait

The new HasActivity trait combines the previously separate LogsActivity and CausesActivity traits into one. Adding this trait to your model is all you need to start tracking created, updated, and deleted events automatically:

use Spatie\Activitylog\Models\Concerns\HasActivity;
 
class User extends Model
{
use HasActivity;
}

With HasActivity, the model can both trigger logged events (as a subject) and be associated as the causer of activity on other models. The individual traits remain available if you need them separately.

Activity Buffering

By default, every logged activity fires an immediate INSERT query. If your application logs many activities in a single request — for example, during batch model updates — this can add up quickly. The new buffering feature collects activities in memory and flushes them in a single bulk query after the response is sent.

Enable it in your .env:

ACTIVITYLOG_BUFFER_ENABLED=true

Or directly in config/activitylog.php:

'buffer' => [
'enabled' => true,
],

No other code changes are required. Both automatic model event logging and manual activity()->log() calls are buffered automatically.

A few things to keep in mind:

  • No ID until flush — buffered activities won't have a database ID until the buffer is flushed, so don't enable this if you need the ID immediately after logging.
  • Octane compatible — the buffer is a scoped binding, so it resets between requests automatically.
  • Queue compatible — the buffer is flushed after each job completes (or fails), so activities logged inside a job are bulk inserted when the job finishes.

Default Causer

Activity::defaultCauser() sets a default causer for logged activities. Pass just a model to set a persistent default, or pass a model and a closure to scope the causer to that callback:

Activity::defaultCauser($adminUser, function () use ($post) {
activity()->performedOn($post)->log('published');
activity()->performedOn($post)->log('notified subscribers');
});

attribute_changes Column

Model change tracking now uses a dedicated attribute_changes database column instead of storing changes inside the general-purpose properties JSON column. The $activity->changes() method has been replaced by direct property access:

// v4
$activity->changes();
 
// v5
$activity->attribute_changes;

The properties column still exists for custom data set with withProperties() or withProperty().

Breaking Changes

Several method and config names changed for consistency. Notable renames:

Old New
activities() activitiesAsSubject()
actions() activitiesAsCauser()
dontSubmitEmptyLogs() dontLogEmptyChanges()
withoutLogs() withoutLogging()
getExtraProperty() getProperty()
ACTIVITY_LOGGER_ENABLED env ACTIVITYLOG_ENABLED
delete_records_older_than_days config clean_after_days

The batch and pipe systems have been removed entirely, including LogBatch, Activity::batch(), LoggablePipe, and EventLogBag. The table_name and database_connection config options are also gone.

Version 5 requires PHP 8.4+ and Laravel 12+. The full upgrade guide and changelog are available on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud

The latest

View all →
Scheduler List: A Web Dashboard for Laravel's Scheduled Tasks image

Scheduler List: A Web Dashboard for Laravel's Scheduled Tasks

Read article
Community Laravel Extension for Zed image

Community Laravel Extension for Zed

Read article
Advanced Eloquent Query Filtering with Filterable image

Advanced Eloquent Query Filtering with Filterable

Read article
Bulk Job Dispatching with Bus::bulk() in Laravel 13.13 image

Bulk Job Dispatching with Bus::bulk() in Laravel 13.13

Read article
Audit Laravel Apps for Security Issues with Checkpoint image

Audit Laravel Apps for Security Issues with Checkpoint

Read article
In-Memory Eloquent Models with Truffle image

In-Memory Eloquent Models with Truffle

Read article