Activity log

An audit trail through spatie/laravel-activitylog, with an admin viewer that filters by log stream, causer and date range. Which models write to it is your decision, one trait per model.

The activity log: labelled log stream, user email and date filters over a table of events, each with its log name as a small badge
/admin/activity. The log name sits under each event as a badge.

What it adds

FilePurpose
app/Livewire/Admin/Activity/ActivityViewer.php and its viewThe paginated viewer

The tables and the Activity model come from the Spatie package.

Install

php artisan ui-kit:install-module activity-log

Composer: spatie/laravel-activitylog:^4.10.

What the installer changes

  • Publishes the package's migrations and runs them, creating activity_log.
  • Adds one route between the markers in routes/admin.php and an Activity nav entry in config/admin.php.

Routes

RouteName
GET /admin/activityadmin.activity.index

Logging something

Add the trait to each model you want recorded and say what to log:

use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

class Order extends Model
{
    use LogsActivity;

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()->logOnly(['status', 'total'])->logOnlyDirty();
    }
}

Manual entries work too: activity()->causedBy($user)->performedOn($order)->log('refunded'), or activity('billing') to write to a log stream of its own.

The screen

Four labelled filters sit over the table: Log stream (every log_name in the table), User email, From and To. The table has When, Who (the causer's name and email), Event, Subject and Description columns. The log name is not a column of its own: it is a small outline badge under the event, default when the entry has none. The page changes through Livewire and keeps the filters.

Removing

Delete the Livewire component and view, remove the route line and the nav entry between their markers, drop the activity_log table, remove activity-log from installed_modules, and composer remove spatie/laravel-activitylog.

Was this page helpful?