PHPackages                             dibakar/activity-scope - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. dibakar/activity-scope

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

dibakar/activity-scope
======================

A robust and flexible activity logging system for Laravel Eloquent models with fluent building, auto-actor resolution, and privacy-first features.

1.0.0(4mo ago)01MITPHPPHP &gt;=8.1CI passing

Since Dec 25Pushed 4mo agoCompare

[ Source](https://github.com/dibakarmitra/activity-scope)[ Packagist](https://packagist.org/packages/dibakar/activity-scope)[ RSS](/packages/dibakar-activity-scope/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (5)Versions (2)Used By (0)

Activity Scope — Audit Trail &amp; Activity Logging for Laravel
===============================================================

[](#activity-scope--audit-trail--activity-logging-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7ce392db4bfb3d82a512d3969dbc348914ab510e52e15590bba73132156ed5cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646962616b61722f61637469766974792d73636f70652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dibakar/activity-scope)[![Total Downloads](https://camo.githubusercontent.com/377be1db9fa5c9d53db45262cfddf259d5ab7b14ca1d8db0103e2c83ecf371f7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646962616b61722f61637469766974792d73636f70652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dibakar/activity-scope)[![License](https://camo.githubusercontent.com/775a625b6fd61225b0b27a7df449d326c19ee63cb9d5c928536929285fede432/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f646962616b61722f61637469766974792d73636f70653f7374796c653d666c61742d737175617265)](https://github.com/dibakarmitra/activity-scope)[![PHP Version](https://camo.githubusercontent.com/5689d311d17601e94f905e08d936a835d6c7d0835e5d92f90018b4c90351bedf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f646962616b61722f61637469766974792d73636f70653f7374796c653d666c61742d737175617265)](https://php.net/)[![Laravel Version](https://camo.githubusercontent.com/9e1d1db04318b49a071768f2c4f5e8df58e9f3fdd40a861c379a523219241249/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31302e782b2d6f72616e67653f7374796c653d666c61742d737175617265)](https://laravel.com/)

📋 Table of Contents
-------------------

[](#-table-of-contents)

- [About](#about)
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
    - [Basic Usage](#basic-usage)
    - [Model Integration](#model-integration)
    - [Query Scopes](#query-scopes)
    - [Advanced Features](#advanced-features)
    - [Security &amp; Privacy](#security--privacy)
    - [Events &amp; Hooks](#events--hooks)
- [Examples](#examples)
- [API Reference](#api-reference)
- [Testing](#testing)
- [Contributing](#contributing)
- [Changelog](#changelog)
- [License](#license)
- [Support](#support)

📖 About {#about}
----------------

[](#-about-about)

Activity Scope is a high-performance **audit trail** and **activity logging** system for Laravel. It allows you to effortlessly track user actions, monitor Eloquent model changes, and maintain a historical record of your application's state—all while prioritizing privacy and high performance.

> **📚 Check out the [API.md](API.md)** for a full list of available builder methods and scopes.

✨ Features {#features}
----------------------

[](#-features-features)

- **🔧 Fluent Activity Builder**: Expressive, chainable API for logging any event with ease
- **🤖 Smarter Auto-Actor**: Automatically resolves the current authenticated user as the actor
- **🔒 Privacy First**: Built-in IP anonymization and recursive sensitive metadata scrubbing
- **🧩 Independent Modular Traits**:
    - `HasActivities`: Core relationships and log retrieval
    - `LogsActivity`: Zero-config auto-logging for Eloquent events
- **🔗 Relationship Support**: Log activity across model relationships or on multiple models at once
- **📝 Human-Readable Logs**: Integrated `MessageBuilder` to transform raw logs into meaningful alerts
- **🛡️ Security Ready**: Dedicated security event logging for audits
- **🎯 Type Safety**: Built with PHP 8.1+ features and strict typing

📋 Requirements {#requirements}
------------------------------

[](#-requirements-requirements)

- **PHP**: &gt;= 8.1
- **Laravel**: ^10.0|^11.0|^12.0
- **Illuminate/Database**: ^10.0|^11.0|^12.0
- **Illuminate/Support**: ^10.0|^11.0|^12.0

🚀 Installation {#installation}
------------------------------

[](#-installation-installation)

You can install the package via composer:

```
composer require dibakar/activity-scope
```

### Quick Setup

[](#quick-setup)

Publish the config and migrations with our handy install command:

```
php artisan activityscope:install
php artisan migrate
```

### Manual Setup

[](#manual-setup)

Alternatively, you can publish them manually via `vendor:publish`:

```
# Publish config file
php artisan vendor:publish --tag=activityscope-config

# Publish migration files
php artisan vendor:publish --tag=activityscope-migrations

# Run migrations
php artisan migrate
```

⚙️ Configuration {#configuration}
---------------------------------

[](#️-configuration-configuration)

After publishing the config file, you can customize the behavior in `config/activityscope.php`:

```
return [
    // Enable/disable activity logging
    'enabled' => env('ACTIVITY_LOGGER_ENABLED', true),

    // Enable automatic model event logging
    'auto_log' => env('ACTIVITY_LOGGER_AUTO_LOG', false),
    'auto_log_events' => ['created', 'updated', 'deleted'],

    // Privacy & Security settings
    'privacy' => [
        'sanitize_data' => env('ACTIVITY_SANITIZE_DATA', true),
        'sensitive_fields' => [
            'password', 'password_confirmation', 'secret', 'token',
            'key', 'card', 'cvv', 'api_key', 'credit_card', 'ssn'
        ],
        'track_ip_address' => env('ACTIVITY_TRACK_IP', true),
        'anonymize_ip' => env('ACTIVITY_ANONYMIZE_IP', false),
    ],
];
```

🛠 Usage {#usage}
----------------

[](#-usage-usage)

### Basic Usage

[](#basic-usage)

#### Using the Global Helper

[](#using-the-global-helper)

The most common way to log activity is using the `activity()` helper:

```
use App\Models\Post;
use App\Models\User;

$user = auth()->user();
$post = Post::find(1);

// Simple activity logging
activity()
    ->on($post)
    ->did('published')
    ->log();

// With additional context
activity()
    ->on($post)
    ->did('updated')
    ->with([
        'title' => $post->title,
        'scheduled_at' => now()->addDays(7),
        'editor' => 'tinymce'
    ])
    ->log();

// Using helper methods for common actions
activity()
    ->created($post) // Equivalent to ->on($post)->did('created')
    ->by($user)
    ->success()
    ->log();
```

#### Automatic Actor Resolution

[](#automatic-actor-resolution)

By default, the package automatically attaches `auth()->user()` to any activity. You can override this manually:

```
// Manual actor specification
activity()
    ->by($admin)
    ->on($user)
    ->did('suspended')
    ->with(['reason' => 'Policy violation'])
    ->log();

// System-performed activities
activity()
    ->bySystem()
    ->did('backup_completed')
    ->with(['size' => '2.5GB', 'duration' => '45s'])
    ->log();

// Guest user activities
activity()
    ->byGuest()
    ->on($product)
    ->did('viewed')
    ->log();

// Job/Queue context
activity()
    ->byJob('ProcessVideoUpload')
    ->on($video)
    ->did('processed')
    ->log();
```

### Model Integration

[](#model-integration)

#### Automatic Model Logging

[](#automatic-model-logging)

Add the `LogsActivity` trait to your model for automatic auditing of `created`, `updated`, and `deleted` events:

```
