Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Activitylog Laravel Package

spatie/laravel-activitylog

Log user and model activity in Laravel with a simple API. Automatically record Eloquent events, track subjects and causers, attach custom properties, and query everything via the Activity model. Stores logs in the activity_log table.

View on GitHub
Deep Wiki
Context7

title: Logging activity weight: 1

This is the most basic way to log activity:

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

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

$lastActivity = Activity::all()->last(); //returns the last logged activity

$lastActivity->description; //returns 'Look mum, I logged something';

Setting a subject

You can specify on which object the activity is performed by using performedOn():

activity()
   ->performedOn($someContentModel)
   ->log('edited');

$lastActivity = Activity::all()->last(); //returns the last logged activity

$lastActivity->subject; //returns the model that was passed to `performedOn`;

The performedOn() method has a shorter alias: on()

Setting a causer

You can set who or what caused the activity by using causedBy():

activity()
   ->causedBy($userModel)
   ->performedOn($someContentModel)
   ->log('edited');

$lastActivity = Activity::all()->last(); //returns the last logged activity

$lastActivity->causer; //returns the model that was passed to `causedBy`;

The causedBy() method has a shorter alias: by()

If you're not using causedBy(), the package will automatically use the logged in user.

If you don't want to associate a model as causer of activity, you can use causedByAnonymous() (or the shorter alias: byAnonymous()).

Setting custom properties

You can add arbitrary metadata to an activity by using withProperties(). This is separate from attribute_changes, which the package uses to store old/new model attribute values when logging model events.

activity()
   ->causedBy($userModel)
   ->performedOn($someContentModel)
   ->withProperties(['key' => 'value'])
   ->log('edited');

$lastActivity = Activity::all()->last(); //returns the last logged activity

$lastActivity->getProperty('key'); //returns 'value'

Activity::where('properties->key', 'value')->get(); // get all activity where the `key` custom property is 'value'

Setting custom created date

You can set a custom activity created_at date time by using createdAt()

activity()
    ->causedBy($userModel)
    ->performedOn($someContentModel)
    ->createdAt(now()->subDays(10))
    ->log('created');

Setting custom event

You can set a custom activity event by using event()

activity()
    ->causedBy($userModel)
    ->performedOn($someContentModel)
    ->event('verified')
    ->log('The user has verified the content model.');

Tap Activity before logged

You can use the tap() method to fill properties and add custom fields before the activity is saved.

use Spatie\Activitylog\Contracts\Activity as ActivityContract;

activity()
   ->causedBy($userModel)
   ->performedOn($someContentModel)
   ->tap(function(ActivityContract $activity) {
      $activity->my_custom_field = 'my special value';
   })
   ->log('edited');

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

$lastActivity->my_custom_field; // returns 'my special value'
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport