Your Guide to Laravel Excellence

Model Change Tracking in Laravel 11 - Laravel Auditing

Model Change Tracking in Laravel 11 - Laravel Auditing

If you want to keep track of your application you can achieve this by laravel auditing package. We will implement laravel audit package in laravel 11, which is laravel latest version.

Laravel Auditing package allows you to keep track of changes made to records, ensuring you can monitor and review modifications to your application's data. It offers a way to audit various events that occur on your models like ‘created’,’updated’,'deleted’,’restored’. This package is particularly useful for applications that require an audit trail for debugging, historical analysis,etc.

Installation

This package should be installed via composer.


composer require owen-it/laravel-auditing

php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="config"

php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="migrations"

php artisan migrate

php artisan config:clear

php artisan cache:clear

Setting Up the Model

Make sure your Eloquent model implements AuditableContract and uses the Auditable trait.


<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
use OwenIt\Auditing\Auditable;

class Post extends Model implements AuditableContract
{
    use HasFactory,Auditable;
    protected $fillable = ['title', 'content'];
}

If you go to the config/audit.php.You will this :

'events' => [
    'created',
    'updated',
    'deleted',
    'restored'
],

It means it will create the audit for these 4 events.When any of the events triggered the audit will be created.

Test the Auditing

"Eloquent events fired from a Job or from the console (i.e. migrations, tests, commands, Tinker, ...), WILL NOT be audited by default." Reference


Route::get('/', function () {
    $post = Post::create(['title' => 'New Post', 'content' => 'Post content']);
    $audits = $post->audits;      // Fetch audit logs
    dd($audits, $post);
});

Recommeded Posts

Faster Query with `whereIntegerInRaw` Method

Faster Query with `whereIntegerInRaw` Method

Faster Query with `whereIntegerInRaw` Method

2 months ago Read article →
How to Easily Add ‘Add to Calendar’ Functionality in Laravel with Spatie Calendar-Links

How to Easily Add ‘Add to Calendar’ Functionality in Laravel with Spatie Calendar-Links

Learn how to add an Add to Calendar feature in your Laravel app using Spatie's Calendar-Links package. This guide covers step-by-step installation, generating Google Calendar, iCal, and Outlook links, and including event details like organizer, attendees, and reminders for a complete user experience.

2 months ago Read article →
Learn Laravel Scopes and simplify Eloquent Queries

Learn Laravel Scopes and simplify Eloquent Queries

Learn Laravel Scopes and simplify Eloquent Queries

2 months ago Read article →
Laravel query() and toQuery() Explained in Detail with Practical Example

Laravel query() and toQuery() Explained in Detail with Practical Example

Learn the difference between query() and toQuery() in Laravel with detailed explanations and practical examples. Understand how to optimize your queries for performance in Laravel applications

2 months ago Read article →