Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

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
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
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
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
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

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

Shift
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
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
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Laravel Mobile Pass: Generate Apple Wallet and Google Wallet Passes image

Laravel Mobile Pass: Generate Apple Wallet and Google Wallet Passes

Read article
PHPverse 2026 Returns June 9th image

PHPverse 2026 Returns June 9th

Read article
LLPhant: A PHP Generative AI Framework Inspired by LangChain image

LLPhant: A PHP Generative AI Framework Inspired by LangChain

Read article
Debounceable Queued Jobs in Laravel 13.6.0 image

Debounceable Queued Jobs in Laravel 13.6.0

Read article
Build Custom Middleware for Query Performance Monitoring and Optimization in Laravel with MongoDB image

Build Custom Middleware for Query Performance Monitoring and Optimization in Laravel with MongoDB

Read article
Laravel API Starter Kits Are Coming Soon! image

Laravel API Starter Kits Are Coming Soon!

Read article