A comprehensive Laravel package for logging and tracking user activity in your application.
- 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
You can install the package via composer:
composer require escarter/activity-logPublish the config file:
php artisan vendor:publish --tag=activity-log-configPublish the migrations:
php artisan vendor:publish --tag=activity-log-migrationsPublish the views (optional):
php artisan vendor:publish --tag=activity-log-viewsRun the migrations:
php artisan migrateTo 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']);
}
}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);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');
});You can clean old logs using the provided command:
php artisan activity-log:clean --days=30Or you can schedule it in your App\Console\Kernel:
protected function schedule(Schedule $schedule)
{
$schedule->command('activity-log:clean')->daily();
}The MIT License (MIT). Please see License File for more information.