Laravel User Activity is a package designed to track and monitor user activity in a Laravel application. It logs actions performed by users, such as logging in, updating profiles, deleting resources, or any other actions defined in your application. This can be useful for auditing, security, or analytics purposes.
Key Features:
- Action Logging: Automatically logs user actions like create, update, delete, and custom events.
- Customizable Events: Allows tracking of custom user activities by defining specific actions.
- Database Storage: Stores activity logs in the database for later analysis.
- User Association: Associates activities with authenticated users for a detailed audit trail.
- IP Address and Browser Data: Captures additional data such as the user’s IP address, device, and browser used during the activity.
- Dashboard: Some packages provide an optional dashboard to visualize user activity.
- Flexible Configuration: Customize what data to track and log based on your application’s needs.
Installation
- Install the package via Composer:
bash
1composer require haruncpi/laravel-user-activity - Publish the package’s configuration and migration files:
bash
1php artisan vendor:publish --tag=user-activity - Run the migration to create the
user_activities
table in the database:bash
1php artisan migrate
Basic Setup
By default, the package will log activities like user login, logout, resource creation, update, and deletion. It is customizable to track additional events if needed.
The user_activities
table will contain fields like:
- User ID: The ID of the user who performed the action.
- Action: The type of action performed (e.g., login, update, delete).
- Description: A brief description of the action.
- IP Address: The IP address from which the action was performed.
- User Agent: Information about the browser or device used.
Tracking Custom User Activities
You can log custom user activities by calling the activity()
helper method within your application logic:
1 2 3 4 5 |
activity() ->performedOn($model) ->causedBy(auth()->user()) ->withProperties(['custom' => 'data']) ->log('User viewed a resource'); |
This logs a custom action describing the user’s interaction with the system.
Displaying User Activities
You can easily retrieve and display the logged activities. For example, to list all user activities in a view:
1 2 3 |
$activities = \App\Models\UserActivity::latest()->get(); return view('user-activities', compact('activities')); |
In your view, you can loop through the activities:
1 2 3 4 5 6 7 8 9 |
@foreach ($activities as $activity) <tr> <td>{{ $activity->user->name }}</td> <td>{{ $activity->action }}</td> <td>{{ $activity->created_at }}</td> <td>{{ $activity->ip_address }}</td> <td>{{ $activity->user_agent }}</td> </tr> @endforeach |
Configuration
The package configuration file config/user-activity.php
allows you to customize which actions to track and what additional data to log.
For example, you can specify to log only certain types of user activities, like login, create, update, and delete:
1 2 3 4 5 6 7 8 9 |
return [ 'log_events' => [ 'login', 'logout', 'created', 'updated', 'deleted' ], ]; |
Conclusion
Laravel User Activity is a useful tool for tracking and auditing user actions within your application. Whether you need to monitor user interactions for security purposes or to analyze how users engage with your system, this package provides a simple and customizable solution. With automatic logging of common events and support for custom event tracking, you can gain valuable insights into your application’s user activity.