Laravel Activity Log is a package that allows you to easily record user activities and events in your Laravel application. This logging feature is essential for monitoring user actions, tracking changes, and auditing purposes. It can help you maintain a comprehensive history of user interactions with your application.
Key Features of Laravel Activity Log:
- Automatic Logging: Automatically log user activities like create, update, and delete actions on models.
- Customizable Log Entries: Define what data to log and customize the log entries according to your requirements.
- Event Listener Integration: Listen to model events and log activities based on those events.
- Queryable Log Entries: Easily query and filter activity logs for specific users, actions, or models.
- Soft Deletes: Automatically handle soft-deleted models in your activity logs.
Installation
To get started with Laravel Activity Log, follow these steps:
- Require the Package: Install the package via Composer. One popular choice is
spatie/laravel-activitylog
:
1composer require spatie/laravel-activitylog - Publish Configuration (Optional): If you want to customize the configuration, publish the package’s configuration file:
1php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" - Run Migrations: Run the migration to create the necessary tables for storing activity logs:
1php artisan migrate
Setting Up Activity Logging
- Adding the Activity Logger Trait: To log activities for a model, you need to use the
LogsActivity
trait. For example, in yourUser
model:
1234567891011121314namespace App\Models;use Illuminate\Database\Eloquent\Model;use Spatie\Activitylog\Traits\LogsActivity;class User extends Model{use LogsActivity;protected static $logAttributes = ['name', 'email']; // Specify which attributes to log// Optionally customize the log nameprotected static $logName = 'user';} - Logging Activities Manually: You can also log activities manually by calling the
activity()
helper function:
12345activity()->performedOn($user)->causedBy(auth()->user())->withProperties(['customProperty' => 'value'])->log('User logged in'); - Listening to Model Events: If you want to automatically log activities on specific actions like create, update, and delete, you can set up listeners in your model:
1protected static $recordEvents = ['created', 'updated', 'deleted']; - Querying Activity Logs: You can easily retrieve activity logs using the
Activity
model provided by the package:
123use Spatie\Activitylog\Models\Activity;$activities = Activity::where('causer_id', auth()->id())->get(); - Displaying Activity Logs: You can display the activity logs in your views, showing relevant information like the action taken, the user who performed it, and timestamps.
12345<ul>@foreach($activities as $activity)<li>{{ $activity->description }} by {{ $activity->causer->name }} on {{ $activity->created_at }}</li>@endforeach</ul>
Conclusion
Laravel Activity Log is a powerful tool for tracking user activities in your application. By providing an easy way to log and manage activities, it enhances your application’s audit capabilities and allows for better user monitoring.
Additional Considerations
- Documentation: For more detailed information, advanced features, and configuration options, refer to the official Spatie Laravel Activity Log documentation.
- Performance: Consider optimizing your database queries when retrieving activity logs, especially if your application generates a large number of log entries.