Laravel Auditing is a package that allows you to track changes in your Eloquent models by automatically logging changes to the database. This is useful for maintaining an audit trail for models, enabling you to keep track of who made changes, when the changes were made, and what the changes were.
Key Features of Laravel Auditing:
- Automatic Change Tracking: Automatically logs changes to model attributes.
- User Tracking: Optionally associates changes with a specific user.
- Customizable Log Format: Customize the format and content of the audit logs.
- Support for Soft Deletes: Tracks changes for soft-deleted models.
- Integrated with Eloquent: Seamless integration with Laravel’s Eloquent ORM.
Installation
To get started with Laravel Auditing, follow these steps:
- Require the Package: Install the package via Composer:
1composer require owen-it/laravel-auditing - Publish Configuration: You can publish the configuration file using:
1php artisan vendor:publish --provider="OwenIt\Auditing\AuditingServiceProvider" - Run Migrations: Run the migration to create the
audits
table in your database:
1php artisan migrate
Setting Up a Model for Auditing
To enable auditing on a model, you need to use the OwenIt\Auditing\Contracts\Auditable
interface and the OwenIt\Auditing\Auditable
trait in your model.
- Create a Model (if you don’t have one already): For example, if you have a
Post
model, you can set it up as follows:
123456789101112namespace App\Models;use Illuminate\Database\Eloquent\Model;use OwenIt\Auditing\Contracts\Auditable;use OwenIt\Auditing\Auditable as AuditableTrait;class Post extends Model implements Auditable{use AuditableTrait;protected $fillable = ['title', 'content'];}
Configuring the Audit Behavior
You can customize the behavior of the audit logging by modifying the config/audit.php
configuration file. Some common configurations include:
- Setting the User: If you want to automatically associate changes with the authenticated user, you can configure it in
config/audit.php
:
123'user' => ['guard' => 'web', // Specify the guard for user authentication], - Customizing the Auditable Events: You can specify which events to log, such as
created
,updated
, ordeleted
.
Auditing Changes
Once the model is set up for auditing, changes to the model will be automatically logged. For example:
- Creating a New Post: When you create a new post, an audit entry will be created:
1234$post = Post::create(['title' => 'My First Post','content' => 'This is the content of my first post.',]); - Updating a Post: When you update an existing post, another audit entry will be created:
123$post->update(['title' => 'My Updated First Post',]); - Deleting a Post: When you delete a post, it will also log the deletion:
1$post->delete();
Retrieving Audit Logs
You can retrieve the audit logs associated with a model instance using the audits
relationship:
1 2 |
$post = Post::find(1); $audits = $post->audits; // Retrieve all audit logs for the post |
Viewing Audit Logs
You can display audit logs in your views. For example, you could create a Blade view to show the audit logs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
@extends('layouts.app') @section('content') <div class="container"> <h1>Audit Logs for Post: {{ $post->title }}</h1> <table class="table"> <thead> <tr> <th>Event</th> <th>Old Values</th> <th>New Values</th> <th>User ID</th> <th>Created At</th> </tr> </thead> <tbody> @foreach ($post->audits as $audit) <tr> <td>{{ $audit->event }}</td> <td>{{ json_encode($audit->old_values) }}</td> <td>{{ json_encode($audit->new_values) }}</td> <td>{{ $audit->user_id }}</td> <td>{{ $audit->created_at }}</td> </tr> @endforeach </tbody> </table> </div> @endsection |
Conclusion
Laravel Auditing is a powerful package that enables developers to keep track of changes in their Eloquent models easily. By automatically logging changes, it helps maintain a clear audit trail, which is essential for many applications, especially those requiring compliance and accountability.
Additional Considerations
- Documentation: For more detailed information, advanced features, and configuration options, refer to the official Laravel Auditing documentation.
- Customizing Audit Logging: You can further customize how audits are recorded by overriding methods in your model.