laravel-activitylog is a free, open source compliance & risk management project written in PHP and released under MIT. It has 5,900 GitHub stars, 745 forks and 0 open issues, and was last pushed 9 days ago. On this registry it ranks #14 of 45 tracked projects in Compliance & Risk Management, with 5 head-to-head comparisons available. It gained 1 stars over the last 3 tracked days.

What is laravel-activitylog?

laravel-activitylog is a free, MIT-licensed PHP package from Spatie that records user and model activity inside a Laravel application in a single database table, aimed at Laravel developers and teams that need an auditable trail of who changed what.

What it is

The package is spatie/laravel-activitylog, published on Packagist and installed through Composer. It "provides easy to use functions to log the activities of the users of your app", and it can "also automatically log model events". Everything it captures is written to the activity_log table and read back through the Spatie\Activitylog\Models\Activity model. The package automatically registers itself, so a Laravel application gains the model, the helper functions and the storage layer without manual wiring. Documentation lives at https://docs.spatie.be/laravel-activitylog, and the package carries the topics audit, laravel, log, monitoring and php, placing it in the Business Software / Compliance & Risk Management category of this registry.

The concrete problem it solves is the audit trail a Laravel application would otherwise have to build by hand. Without it, a team writes its own log table, its own migration, its own model and its own hooks into Eloquent save events for every model that matters. This package supplies all of that: a published migration that creates activity_log, a model to query it, a fluent helper for manual entries, and automatic capture of model events. When a model is saved after a change, the package records the activity, and the record keeps a reference to the subject that changed along with the causer responsible for it.

Key capabilities

  • Manual logging through the activity() helper, as in activity()->log('Look, I logged something').
  • Fluent context on each entry via performedOn($anEloquentModel), causedBy($user) and withProperties(['customProperty' => 'customValue']).
  • Automatic model event logging: changing and saving a model causes an activity to be written, with a description such as 'updated'.
  • Retrieval and inspection through the Activity model, where subject returns the Eloquent model instance, causer returns the user model, getProperty('customProperty') returns the custom value, and description returns the logged text.
  • Change tracking through attribute_changes, which returns a collection containing attributes and old arrays so previous and current values can be compared.
  • A published migration tagged activitylog-migrations that creates the activity_log table.
  • An optional publishable config file, exposed through the same Spatie\Activitylog\ActivitylogServiceProvider provider.

Who uses it and how

  • Laravel teams that need an audit record of user actions, matching the audit and monitoring topics on the repository.
  • Applications that must show which user changed which record, using causedBy($user) together with performedOn($model).
  • Projects tracking field-level history, reading attribute_changes to compare old values against new ones.
  • Teams using UUID primary keys, which must first adjust the format of the subject_id and causer_id fields in the published migration before running it.
  • Developers debugging model writes, relying on automatic event logging rather than adding logging calls to each save path.

Getting started

Install with composer require spatie/laravel-activitylog, publish the migration using php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations", then create the table with php artisan migrate. The config file can optionally be published through the same provider.

How it compares

No list of paid products that this package replaces is supplied, and no comparable tools are named in the facts. On the evidence available here, it stands alone in this registry.

When to use it — and when not to

A self-hoster must operate the database that holds the activity_log table, publish and run the migration, and keep that table in step with application models. It is not a fit for applications outside the Laravel and PHP stack, since the helpers and the Eloquent model event hooks are framework-specific. The facts describe no user interface, no retention or pruning policy and no hosted option, so teams expecting a ready-made audit dashboard or a managed service should look elsewhere.

project readme (upstream, from github) — read inline
Logo for laravel-activitylog
</a>

Log activity inside your Laravel app

Latest Version on Packagist GitHub Workflow Status Check & fix styling Total Downloads

The spatie/laravel-activitylog package provides easy to use functions to log the activities of the users of your app. It can also automatically log model events. The package stores all activity in the activity_log table.

Here's a demo of how you can use it:

activity()->log('Look, I logged something');

You can retrieve all activity using the Spatie\Activitylog\Models\Activity model.

Activity::all();

Here's a more advanced example:

activity()
   ->performedOn($anEloquentModel)
   ->causedBy($user)
   ->withProperties(['customProperty' => 'customValue'])
   ->log('Look, I logged something');

$lastLoggedActivity = Activity::all()->last();

$lastLoggedActivity->subject; //returns an instance of an eloquent model
$lastLoggedActivity->causer; //returns an instance of your user model
$lastLoggedActivity->getProperty('customProperty'); //returns 'customValue'
$lastLoggedActivity->description; //returns 'Look, I logged something'

Here's an example on event logging.

$newsItem->name = 'updated name';
$newsItem->save();

//updating the newsItem will cause the logging of an activity
$activity = Activity::all()->last();

$activity->description; //returns 'updated'
$activity->subject; //returns the instance of NewsItem that was saved

Calling $activity->attribute_changes will return this collection:

[
   'attributes' => [
        'name' => 'updated name',
        'text' => 'Lorem',
    ],
    'old' => [
        'name' => 'original name',
        'text' => 'Lorem',
    ],
];

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Documentation

You'll find the documentation on https://spatie.be/docs/laravel-activitylog/introduction.

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the activity log? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.

Installation

You can install the package via composer:

composer require spatie/laravel-activitylog

The package will automatically register itself.

You can publish the migration with:

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

Note: The default migration assumes you are using integers for your model IDs. If you are using UUIDs, or some other format, adjust the format of the subject_id and causer_id fields in the published migration before continuing.

After publishing the migration you can create the activity_log table by running the migrations:

php artisan migrate

You can optionally publish the config file with:

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

Changelog

Please see CHANGELOG for more information about recent changes.

Upgrading

Please see UPGRADING for details.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Credits

Special thanks to Ahmed Nagi for all the work he put in v4 and to Caneco for the original logo.

License

The MIT License (MIT). Please see License File for more information.

Frequently asked questions

Is laravel-activitylog free to use?

laravel-activitylog is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does laravel-activitylog do?

Log activity inside your Laravel app

What is laravel-activitylog written in?

laravel-activitylog is primarily written in PHP. Its source is publicly available at https://github.com/spatie/laravel-activitylog, and it has 5,900 GitHub stars.