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: Customizing actions weight: 7

The package uses action classes for its core operations. You can extend these to customize behavior.

Available actions

LogActivityAction

Called every time an activity is logged. Handles description placeholder replacement, transforming changes, calling beforeActivityLogged() on the subject, and saving the activity to the database.

CleanActivityLogAction

Called by the activitylog:clean command. Handles deleting old activity records.

Overriding an action

Create a class that extends the original action and override the protected methods you want to customize:

use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Actions\LogActivityAction;

class CustomLogActivityAction extends LogActivityAction
{
    protected function save(Model $activity): void
    {
        // Example: dispatch to queue instead of saving synchronously
        dispatch(fn () => $activity->save());
    }
}

Then register it in the config:

// config/activitylog.php
'actions' => [
    'log_activity' => \App\Actions\CustomLogActivityAction::class,
    'clean_log' => \Spatie\Activitylog\Actions\CleanActivityLogAction::class,
],

Manipulating changes

To modify the changes array before it's saved (e.g. removing sensitive fields), override transformChanges():

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Spatie\Activitylog\Actions\LogActivityAction;

class RedactPasswordAction extends LogActivityAction
{
    protected function transformChanges(Model $activity): void
    {
        $changes = $activity->attribute_changes?->toArray() ?? [];

        Arr::forget($changes, ['attributes.password', 'old.password']);

        $activity->attribute_changes = collect($changes);
    }
}

Overridable methods

LogActivityAction

Method Description
resolveDescription($activity, $description) Resolves placeholders like :subject.name in the description
transformChanges($activity) Modify the changes array before saving (default: no-op)
beforeActivityLogged($activity) Calls beforeActivityLogged() on the subject model if it exists
save($activity) Saves the activity to the database
replacePlaceholders($description, $activity) Performs the actual placeholder replacement

CleanActivityLogAction

Method Description
getCutOffDate($maxAgeInDays) Calculates the date before which records should be deleted
deleteOldActivities($cutOffDate, $logName) Performs the actual deletion query
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