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.
The CauserResolver class handles resolving who caused an activity. It is registered as a scoped binding (per request) in the container.
For most use cases, you should use the Activity facade instead of interacting with CauserResolver directly:
use Spatie\Activitylog\Facades\Activity;
// Scoped causer (recommended)
Activity::defaultCauser($admin, function () {
// all activities here will have $admin as causer
});
// Global causer
Activity::defaultCauser($admin);
If you need lower-level control, resolve the CauserResolver from the container:
use Spatie\Activitylog\Support\CauserResolver;
// Custom resolution callback
app(CauserResolver::class)->resolveUsing(function ($subject) {
return User::find(1);
});
Note: setCauser() takes priority over resolveUsing().
public function resolve(Model | int | string | null $subject = null): ?Model;
public function resolveUsing(Closure $callback): static;
public function setCauser(?Model $causer): static;
public function withCauser(?Model $causer, Closure $callback): mixed;
How can I help you explore Laravel packages today?