Skip to content

Activity Logging

Kompass ships the LogsActivity trait to track all model changes. It's a wrapper around the spatie/laravel-activitylog package — you'll need that package installed to use it.

Optional feature

Activity logging is optional. If spatie/laravel-activitylog is not in your composer.json, the LogsActivity trait is a no-op.

Setup

Install the Spatie package:

bash
composer require spatie/laravel-activitylog

Then run the migration:

bash
php artisan migrate

Enabling on a Model

Add the LogsActivity trait to any Eloquent model:

php
use Secondnetwork\Kompass\Traits\LogsActivity;

class Page extends Model
{
    use LogsActivity;
}

Kompass automatically logs all attributes on create, update, and delete — no further configuration needed.

Works with any model

You can apply LogsActivity to any model — Page, Article, BlogPost, Product, or your own custom models.

Querying Activity

Use the Activity model from Spatie to query the log:

php
use Spatie\Activitylog\Models\Activity;

// All activities
Activity::latest()->get();

// For a specific model
Activity::where('subject_type', Page::class)
    ->where('subject_id', $page->id)
    ->get();

// By a specific user
Activity::where('causer_type', User::class)
    ->where('causer_id', $user->id)
    ->get();

Activity Record

Each activity entry contains the following properties (defined by Spatie):

PropertyDescription
log_nameLog name (defaults to default)
descriptionAction: created, updated, or deleted
subjectThe changed model (polymorphic)
causerThe user who made the change (polymorphic)
propertiesJSON with old and attributes values
created_atTimestamp

Before & after values included

The properties field stores both the previous (old) and new (attributes) values for every update, so you can diff exactly what changed.

Cleaning Old Logs

Spatie's activity log includes an Artisan command to prune stale records. Schedule it in routes/console.php:

bash
php artisan activitylog:clean

Or add it to your scheduler:

php
use Illuminate\Support\Facades\Schedule;

Schedule::command('activitylog:clean')->daily();

Released under the MIT License.