escarter / activity-log
A Laravel package for logging user activity with admin and user views
1.0.1.0
2025-04-21 10:15 UTC
Requires
- php: ^8.1
- ext-json: *
- illuminate/database: ^10.48|^11.0
- illuminate/events: ^10.48|^11.0
- illuminate/support: ^10.48|^11.0
Requires (Dev)
- fakerphp/faker: ^1.9
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.0|^7.0|^8.0
- phpunit/phpunit: ^9.5|^10.0
Suggests
- livewire/livewire: Required if using Livewire components
- spatie/laravel-activitylog: For more advanced logging features
README
A comprehensive Laravel package for logging and tracking user activity in your application.
Features
- Track model changes (create, update, delete)
- Log user login events
- View activity logs for specific users or models
- Admin dashboard for all activity logs
- User-specific activity log views
- Configurable retention period
- Bootstrap-styled components
- Livewire integration
Installation
You can install the package via composer:
composer require escarter/activity-log
Configuration
Publish the config file:
php artisan vendor:publish --tag=activity-log-config
Publish the migrations:
php artisan vendor:publish --tag=activity-log-migrations
Publish the views (optional):
php artisan vendor:publish --tag=activity-log-views
Run the migrations:
php artisan migrate
Usage
Tracking model changes
To track changes to a model, add the LogsActivity
trait to the model:
use Escarter\ActivityLog\Traits\LogsActivity; use Illuminate\Database\Eloquent\Model; class Post extends Model { use LogsActivity; // Optional: customize which events to log public function shouldLogActivity(string $event): bool { // Only log creates and updates return in_array($event, ['created', 'updated']); } }
Manual logging
You can manually log activities:
use Escarter\ActivityLog\ActivityLogFacade as ActivityLog; use Escarter\ActivityLog\DTOs\ActivityLogData; // Using facade ActivityLog::log(new ActivityLogData( logName: 'orders', description: 'Order was shipped', subject: $order, causer: auth()->user(), event: 'shipped' )); // Log model events ActivityLog::logModel($model, 'archived', auth()->user()); // Log user login ActivityLog::logLogin($user);
Using the Activity Log views
The package includes Livewire components for displaying activity logs:
- Admin view (all logs):
// In your routes/web.php Route::middleware(['auth', 'admin'])->group(function () { Route::get('/admin/activity-logs', function () { return view('activity-log::livewire.admin-activity-log'); })->name('admin.activity-logs'); });
- User view (user's own logs):
// In your routes/web.php Route::middleware(['auth'])->group(function () { Route::get('/my-activity', function () { return view('activity-log::livewire.user-activity-log'); })->name('user.activity-logs'); });
Cleaning old logs
You can clean old logs using the provided command:
php artisan activity-log:clean --days=30
Or you can schedule it in your App\Console\Kernel
:
protected function schedule(Schedule $schedule) { $schedule->command('activity-log:clean')->daily(); }
License
The MIT License (MIT). Please see License File for more information.