karjah / laravel-activity-log
Simple way to log activities in Laravel.
Requires
- php: ^8.4
- laravel/framework: ^12.0|^13.0
This package is auto-updated.
Last update: 2026-04-22 19:22:59 UTC
README
A lightweight, fluent activity logging package for Laravel.
๐ Installation
Run these commands in your terminal:
composer require karjah/laravel-activity-log php artisan migrate
๐ Usage Examples
1. Global Helper
activity('auth') ->withProperties(['ip' => '127.0.0.1']) ->log('User logged in');
2. Using the Facade
use ActivityLog; ActivityLog::name('orders')->log('New order placed');
Manually setting the User (Causer)
By default, the package uses Auth::id(). To override this (e.g., in a Job or for Admin actions):
//Pass in the user object activity()->causedBy($user)->log('Manual action logged'); // User the user's id activity()->causedBy(6)->log('Manual action logged');
3. Custom Caused By
By default, the current logged in user is used. To use a different user, pass in the id of a user or the User object.
activity()->name('orders')->causedBy(2)->log('New order placed'); activity()->name('orders')->causedBy($adminUser)->log('New order placed');
๐ Querying & Filtering Logs
Since the package uses a standard Eloquent model, you can filter logs using familiar Laravel syntax.
Filter by Log Name
Retrieve all logs for a specific area of your app, such as 'auth' or 'orders'.
use Karjah\ActivityLog\Models\ActivityLog; $authLogs = ActivityLog::where('log_name', 'auth')->get(); $userLogs = ActivityLog::logName('user')->get();
Filter by User
$userLogs = ActivityLog::where('user_id', 1)->latest()->get(); $userLogs = ActivityLog::whereUser(1)->get();
Filter by JSON Properties
// Find logs where the stored 'status' property is 'failed' $failedLogs = ActivityLog::where('properties->status', 'failed')->get();
๐งน Cleaning Old Logs
To prevent your database from getting too large, you can clean up old logs manually or automatically.
Manual Cleanup
Run this command to delete logs older than 30 days (default):
php artisan activitylog:clean
To specify a different timeframe
php artisan activitylog:clean --days=7
Automatic Cleanup (Scheduling)
In your main project's routes/console.php (or app/Console/Kernel.php), schedule the command to run daily:
use Illuminate\Support\Facades\Schedule; Schedule::command('activitylog:clean --days=30')->daily();