Skip to content

zairakai/laravel-activity ​

Pivot activity logging for Eloquent many-to-many relationships, built on top of Spatie Laravel Activity Log.

Documentation: laravel-activity-a54436.gitlab.io


Features ​

  • Fluent API — chainable ->activity()->by()->withMessage()->sync() on any BelongsToMany relation
  • Automatic diffing — logs attached, detached, and unchanged IDs on every sync operation
  • Actor tracking — ->by($user) records who performed the action
  • Custom messages — ->withMessage('Assigned roles') for human-readable log entries
  • Zero configuration — no config file needed, just composer require

Requirements ​

  • spatie/laravel-activitylog must be installed and configured

Install ​

bash
composer require zairakai/laravel-activity

Usage ​

php
use App\Models\User;

$user = User::find(1);

// Log a sync operation on a BelongsToMany relationship
$user->roles()
    ->activity()
    ->by(auth()->user())
    ->withMessage('Assigned roles')
    ->sync([1, 2, 3]);

// Without an actor
$user->permissions()
    ->activity()
    ->withMessage('Permissions updated')
    ->sync([10, 20]);

// Attach and detach also fire activity
$user->tags()->activity()->attach(5);
$user->tags()->activity()->detach(5);

The activity log will record:

  • attached: IDs that were added
  • detached: IDs that were removed
  • unchanged: IDs that were already present

Upgrading to v3.0.0 ​

This release moves the underlying dependency to spatie/laravel-activitylog v5.

Code impact on this package is minimal (an internal namespace move), but spatie ships no automatic migration for existing installs — only a fresh-install migration stub. If your application already has an activity_log table in production, you must write and run your own migration before deploying this version:

  • add attribute_changes (json, nullable) and event (string, nullable)
  • backfill attribute_changes from the existing properties data as needed
  • drop batch_uuid (the batch system was removed in v5)

See spatie/laravel-activitylog's upgrade guide for the full column mapping. Do this migration before bumping this package past ^2.0 in your composer.json.


Development ​

bash
make quality        # pint + phpstan + rector + insights + markdownlint + shellcheck
make quality-fast   # pint + phpstan + markdownlint
make test           # phpunit / pest

Statistics ​

Contributing ​

Contributions are welcome. Please read CONTRIBUTING.md for the project-specific workflow and quality standards.


Getting Help ​

Made with ❤️ by Zairakai

Documentation of 3.0.3 , built from 00cc945 on 3.0.3 by pipeline #2949084910 on 2026-10-11.