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: Before logging hook weight: 8

You can register global callbacks that run on every activity right before it is saved. This is useful for enriching activities with extra data without creating a custom Activity model.

Registering a callback

Call beforeLogging on the Activity facade, typically in a service provider's boot method:

use Spatie\Activitylog\Facades\Activity;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Activity::beforeLogging(function (\Spatie\Activitylog\Contracts\Activity $activity) {
            $activity->properties = $activity->properties->put('ip', request()->ip());
        });
    }
}

Every activity (both manual activity()->log() calls and automatic model event logging) will now include the IP address in its properties.

Multiple callbacks

You can register multiple callbacks. They run in the order they were registered:

Activity::beforeLogging(function ($activity) {
    $activity->properties = $activity->properties->put('ip', request()->ip());
});

Activity::beforeLogging(function ($activity) {
    $activity->properties = $activity->properties->put('user_agent', request()->userAgent());
});

Example: batch UUID

If you need to group related activities together (for example, all activities from a single request), you can use the hook to assign a shared identifier:

1. Add the column:

Schema::table('activity_log', function (Blueprint $table) {
    $table->uuid('batch_uuid')->nullable()->index();
});

2. Register the hook:

use Illuminate\Support\Str;
use Spatie\Activitylog\Facades\Activity;

// In your AppServiceProvider::boot()
$batchUuid = (string) Str::uuid();

Activity::beforeLogging(function ($activity) use ($batchUuid) {
    $activity->batch_uuid = $batchUuid;
});

All activities logged during the request will share the same batch_uuid.

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