l0max / laravel-activity-log
A Laravel package to log and store user activities across the application
Requires
- php: ^7.1.3|7.2.*|7.3.*|7.4.*|8.*
- laravel/framework: 8.*|9.*|10.*|11.*
- laravel/passport: ^12.3
README
A simple package to log user activity in Laravel applications. Admins can view all logs, while users can view only their own logs.
Installation
You can install the package via Composer:
composer require l0max/laravel-activity-log
To publish the configuration file, run the following command:
php artisan vendor:publish --provider="L0MAX\ActivityLog\ActivityLogServiceProvider"
This will publish the config file config/activitylog.php
.
Usage
Once the package is installed, you can use the activity log to track user actions. Each time an action is logged, the ActivityLog
model stores the user ID and a description of the action.
Example Usage
use L0MAX\ActivityLog\ActivityLog; ActivityLog::create([ 'user_id' => auth()->id(), 'description' => 'User performed some action.', ]);
Viewing Logs
Admins can view all logs, while regular users can view only their own activity logs.
Policy
A policy is included to handle permissions for viewing logs. Here's a simple example of how to control access to activity logs:
namespace L0MAX\ActivityLog\Policies; use App\Models\User; use L0MAX\ActivityLog\ActivityLog; class ActivityLogPolicy { public function view(User $user, ActivityLog $log) { return $user->hasRole('admin') || $user->id === $log->user_id; } }
Database Migrations
To create the activity_logs
table, run the migrations:
php artisan migrate
Factory
You can generate test data using the factory provided:
ActivityLog::factory()->create();
Testing
You can run the tests with:
vendor/bin/phpunit
Ensure that you have added the proper testing logic in the tests/Feature/
directory.
Customization
You can customize the package to suit your needs. The following features can be extended:
- Custom log formats
- Additional policies
Security
If you discover any security-related issues, please email ankahdonatus@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.