Looking to hire Laravel developers? Try LaraJobs

laravel-activity-log maintained by karjah

Description
Simple way to log activities in Laravel.
Author
Last update
2026/04/22 20:46 (dev-main)
License
Links
Downloads
1

Comments
comments powered by Disqus

Karjah Activity Log

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();