Weave Code
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

Laravel Model Activity Log Laravel Package

foxen/laravel-model-activity-log

Automatically log Eloquent model activity in Laravel: create, update (with attribute diffs), delete, and restore (SoftDeletes). Supports per-model log names, ignoring or redacting attributes, tracks the authenticated user or falls back to “System”, and can prune old entries.

View on GitHub
Deep Wiki
Context7

Laravel Model Activity Log

A simple Laravel package to automatically record basic activity (creation, updates with attribute changes, deletion, restoration) performed on specified Eloquent models, and provide a mechanism for automatically pruning old log entries.

Requirements

Installation

You can install the package via composer:

composer require foxen/laravel-model-activity-log

The package will automatically register its service provider.

To publish the configuration file, run the following command:

php artisan vendor:publish --provider="Foxen\LaravelModelActivityLog\Providers\ActivityLogServiceProvider" --tag="config"

This will create a foxen_activitylog.php file in your config directory, which can be modified as required.

Finally, you need to run the migrations to create the activity_log table:

php artisan migrate

Usage

To enable activity logging for a model, simply use the LogsActivity trait in your model class:

use Illuminate\Database\Eloquent\Model;
use Foxen\LaravelModelActivityLog\Traits\LogsActivity;

class Post extends Model
{
    use LogsActivity;

    // ...
}

The following events are automatically logged: created, updated, deleted. If your model uses SoftDeletes, the restored event is logged as well.

When an action is performed by an authenticated user, they are recorded as the causer. Actions performed without an authenticated user (e.g. from a console command or queue job) are attributed to System.

Customizing the Log Name

By default, the log name is set to default. You can customize this on a per-model basis by adding a protected $activityLogName property to your model:

class Post extends Model
{
    use LogsActivity;

    protected $activityLogName = 'posts';

    // ...
}

Ignoring Attributes

To exclude certain attributes from the activity log when a model is created or updated, you can add a protected $ignoreActivityLogAttributes property to your model:

class Post extends Model
{
    use LogsActivity;

    protected $ignoreActivityLogAttributes = ['updated_at'];

    // ...
}

Redacting Attributes

To redact sensitive attributes from the activity log, you can add a protected $redactedActivityLogAttributes property to your model. The attribute key will be logged, but the value will be replaced with [REDACTED].

class User extends Model
{
    use LogsActivity;

    protected $redactedActivityLogAttributes = ['password', 'remember_token'];

    // ...
}

You can also configure global redacted attributes in the config/foxen_activitylog.php file.

Retrieving Logs

You can retrieve activity logs using the Foxen\LaravelModelActivityLog\Models\Activity model. The package provides several convenient query scopes:

use Foxen\LaravelModelActivityLog\Models\Activity;

// Get all activity for a specific model instance
$activities = Activity::whereSubject($post)->get();

// Get all activity for a specific model type
$activities = Activity::forSubjectType('App\Models\Post')->get();

// Get all activity caused by a specific user
$activities = Activity::whereCauser($user)->get();

// Get all activity for a specific causer type
$activities = Activity::forCauserType('App\Models\User')->get();

// Get all activity for a specific event
$activities = Activity::forEvent('created')->get();

You can access the subject and causer models directly via their relationships:

$activity = Activity::first();

$subject = $activity->subject; // The model that was acted upon
$causer  = $activity->causer;  // The user who performed the action, or null

For updated events, the properties field contains the old and new values of changed attributes:

$activity = Activity::forEvent('updated')->first();

$old = $activity->properties['old']; // ['title' => 'Old Title']
$new = $activity->properties['new']; // ['title' => 'New Title']

Pruning Logs

The package can automatically prune old activity log entries. To enable this, set the prune_activity_log option to true in your config/foxen_activitylog.php file and configure the prune_older_than_days option.

// config/foxen_activitylog.php

return [
    // ...
    'prune_activity_log' => true,
    'prune_older_than_days' => 30,
];

Once enabled, you must schedule the model:prune command in your application's routes/console.php file:

use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune')->daily();

Testing

composer test
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle