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 viewer with filters and a list of events
/admin/activity.

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'). The viewer shows the description, the causer, the subject, the event and the properties of each entry, and filters on the log_name stream, the causer's email and a date range.

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?