# spatie/laravel-activitylog Activity logging package for Laravel. Logs model events and manual activities to a database table. ## 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 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 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