Laravel Packages

Log User Activity in Your Laravel App with Activity Log v5

Published
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.

Sponsored

laravelcloud logo
Laravel Cloud

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

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article