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:
composer require spatie/laravel-activitylogThen run the migration:
php artisan migrateEnabling on a Model
Add the LogsActivity trait to any Eloquent model:
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:
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):
| Property | Description |
|---|---|
log_name | Log name (defaults to default) |
description | Action: created, updated, or deleted |
subject | The changed model (polymorphic) |
causer | The user who made the change (polymorphic) |
properties | JSON with old and attributes values |
created_at | Timestamp |
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:
php artisan activitylog:cleanOr add it to your scheduler:
use Illuminate\Support\Facades\Schedule;
Schedule::command('activitylog:clean')->daily();