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.
In many cases you may want to set the causer for a block of code, for example inside jobs where there's no logged-in user. The Activity facade provides defaultCauser() for this.
Pass a callback as the second argument to scope the causer to that block. The previous causer is automatically restored afterwards:
use Spatie\Activitylog\Facades\Activity;
Activity::defaultCauser($admin, function () {
$product->update(['name' => 'New name']);
// this activity will have $admin as the causer
});
// the previous causer is restored here
This works cleanly in jobs, CLI commands, seeders, and multi-guard setups.
Without a callback, the causer is set for the rest of the request:
use Spatie\Activitylog\Facades\Activity;
Activity::defaultCauser($admin);
$product->update(['name' => 'New name']);
\Spatie\Activitylog\Models\Activity::all()->last()->causer; // $admin
How can I help you explore Laravel packages today?