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 model events, link actions to subjects and causers, store custom properties, and query a dedicated activity_log table for auditing and history.

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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony