Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
activitylog-development
scajal/laravel-react-starter-kit
claude-code
activitylog
audit-logging
model-events
logoptions
eloquent
spatie

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.

Install
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

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).

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 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
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource