activitylog-development
Builds spatie/laravel-activitylog features. Use when logging model events with the LogsActivity trait, configuring LogOptions, logging activities manually, setting causers, or querying and customizing
Files1
View on GitHubSKILL.md.claude/skills/activitylog-development/SKILL.md
GitHubActivitylog Development
Overview
Use spatie/laravel-activitylog to log model events and manual activities to a database table.
When to Activate
- Activate when working with activity, audit, or change logging in Laravel.
- Activate when code references
LogsActivity,CausesActivity,HasActivity,LogOptions, theActivitymodel, theActivityEventenum, or theactivity()helper. - Activate when the user wants to record, query, or customize activity log entries.
Scope
- In scope: model event logging, LogOptions configuration, manual logging, causers, querying activities, custom Activity models and actions, configuration.
- Out of scope: general application logging (
Logfacade), non-Laravel frameworks.
Key Concepts
- Activity: An Eloquent model (
Spatie\Activitylog\Models\Activity) storing log entries with subject, causer, event, attribute_changes, and properties. - Subject: The model being acted upon (polymorphic
subject_type/subject_id). - Causer: The model that caused the action, typically the authenticated user (polymorphic
causer_type/causer_id). - LogOptions: Fluent configuration object returned by
getActivitylogOptions()on models using theLogsActivitytrait. - ActivityEvent: Enum with cases
Created,Updated,Deleted,Restored. attribute_changescolumn: stores{"attributes": {...}, "old": {...}}for tracked model changes.propertiescolumn: stores custom user data set viawithProperties().
Traits
LogsActivity
Add to models to automatically log create/update/delete events. Optionally implement getActivitylogOptions() to configure which attributes to track (defaults to logging events without attribute changes).
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
use Spatie\Activitylog\Support\LogOptions;
class Article extends Model
{
use LogsActivity;
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logFillable()
->logOnlyDirty()
->dontLogEmptyChanges();
}
}
CausesActivity
Add to user/causer models. Provides activitiesAsCauser() relationship.
HasActivity
Combines LogsActivity and CausesActivity. Provides activities(), activitiesAsSubject(), and activitiesAsCauser().
Manual Logging
use Spatie\Activitylog\Enums\ActivityEvent;
activity()
->performedOn($article)
->causedBy($user)
->event(ActivityEvent::Updated)
->withProperties(['key' => 'value'])
->log('Article was updated');
LogOptions Methods
| Method | Description |
|---|---|
logFillable() |
Log all fillable attributes |
logAll() |
Log all attributes |
logOnly(array) |
Log specific attributes |
logExcept(array) |
Exclude attributes |
logOnlyDirty() |
Only log changed attributes |
dontLogEmptyChanges() |
Skip logging when no tracked attributes changed |
dontLogIfAttributesChangedOnly(array) |
Ignore updates that only change these attributes |
useLogName(string) |
Set custom log name |
setDescriptionForEvent(Closure) |
Custom description per event |
useAttributeRawValues(array) |
Store raw (uncast) values |
Querying Activities
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Enums\ActivityEvent;
Activity::forEvent(ActivityEvent::Created)->get();
Activity::causedBy($user)->get();
Activity::forSubject($article)->get();
Activity::inLog('orders')->get();
Setting the causer
Override the causer for a block of code:
use Spatie\Activitylog\Facades\Activity;
Activity::defaultCauser($admin, function () {
// all activities here are caused by $admin
});
// or set globally for the rest of the request
Activity::defaultCauser($admin);
Disabling Logging
activity()->withoutLogging(function () {
// no activities logged here
});
Accessing Changes and Properties
$activity = Activity::latest()->first();
// Tracked model changes (set automatically by LogsActivity)
$activity->attribute_changes; // Collection: {"attributes": {...}, "old": {...}}
// Custom user data (set via withProperties)
$activity->properties; // Collection
$activity->getProperty('key'); // single value
Custom Activity Model
Set activity_model in config/activitylog.php to a class that extends Model and implements Spatie\Activitylog\Contracts\Activity. Use a custom model for custom table names or database connections.
Customizing Actions
The package uses action classes (LogActivityAction, CleanActivityLogAction) that can be extended and swapped via config:
// config/activitylog.php
'actions' => [
'log_activity' => \App\Actions\CustomLogActivityAction::class,
'clean_log' => \App\Actions\CustomCleanAction::class,
],
Custom action classes must extend the originals. Override protected methods (save(), beforeActivityLogged(), resolveDescription(), etc.) to customize behavior.
Configuration
Key config options in config/activitylog.php:
enabled: Master on/off switch (env:ACTIVITYLOG_ENABLED)clean_after_days: Days to keep records foractivitylog:cleancommanddefault_log_name: Default log name (string)default_auth_driver: Auth driver for causer resolutioninclude_soft_deleted_subjects: Include soft-deleted subjectsactivity_model: Custom Activity model classdefault_except_attributes: Globally excluded attributesactions.log_activity: Action class for logging activitiesactions.clean_log: Action class for cleaning old activities