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 activities.
php artisan boost:add-skill scajal/laravel-react-starter-kit
Save this content to: .claude/skills/activitylog-development/SKILL.md
# Activitylog 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`, the `Activity` model, the `ActivityEvent` enum, or the `activity()` 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 (`Log` facade), 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 the `LogsActivity` trait.
- **ActivityEvent**: Enum with cases `Created`, `Updated`, `Deleted`, `Restored`.
- **`attribute_changes`** column: stores `{"attributes": {...}, "old": {...}}` for tracked model changes.
- **`properties`** column: stores custom user data set via `withProperties()`.
## 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).
```php
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
```php
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
```php
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:
```php
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
```php
activity()->withoutLogging(function () {
// no activities logged here
});
```
## Accessing Changes and Properties
```php
$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:
```php
// 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 for `activitylog:clean` command
- `default_log_name`: Default log name (string)
- `default_auth_driver`: Auth driver for causer resolution
- `include_soft_deleted_subjects`: Include soft-deleted subjects
- `activity_model`: Custom Activity model class
- `default_except_attributes`: Globally excluded attributes
- `actions.log_activity`: Action class for logging activities
- `actions.clean_log`: Action class for cleaning old activities
Use spatie/laravel-activitylog to log model events and manual activities to a database table.
LogsActivity, CausesActivity, HasActivity, LogOptions, the Activity model, the ActivityEvent enum, or the activity() helper.Log facade), non-Laravel frameworks.Spatie\Activitylog\Models\Activity) storing log entries with subject, causer, event, attribute_changes, and properties.subject_type/subject_id).causer_type/causer_id).getActivitylogOptions() on models using the LogsActivity trait.Created, Updated, Deleted, Restored.attribute_changes column: stores {"attributes": {...}, "old": {...}} for tracked model changes.properties column: stores custom user data set via withProperties().LogsActivityAdd 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();
}
}
CausesActivityAdd to user/causer models. Provides activitiesAsCauser() relationship.
HasActivityCombines LogsActivity and CausesActivity. Provides activities(), activitiesAsSubject(), and activitiesAsCauser().
use Spatie\Activitylog\Enums\ActivityEvent;
activity()
->performedOn($article)
->causedBy($user)
->event(ActivityEvent::Updated)
->withProperties(['key' => 'value'])
->log('Article was updated');
| 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 |
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();
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);
activity()->withoutLogging(function () {
// no activities logged here
});
$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
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.
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.
Key config options in config/activitylog.php:
enabled: Master on/off switch (env: ACTIVITYLOG_ENABLED)clean_after_days: Days to keep records for activitylog:clean commanddefault_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 activitiesHow can I help you explore Laravel packages today?