Blog Detail

09

Aug
Record the Change Log From Models By Laravel Auditing cover image

arrow_back Record the Change Log From Models By Laravel Auditing

Owen IT Services introduced a package that records the changelog from models in Laravel. It will assist you to understand changes in your Eloquent models, by providing knowledge about potential discrepancies and anomalies that could show business concerns or suspect activities.
Laravel Auditing enables you to keep a history of model changes by simply using a trait. You can easily retrieve the audited data, and it is possible to display it in various ways.

Installation

You can install laravel auditing via Composer. You’ve to execute the following command for the installation:

composer require owen-it/laravel-auditing

Configuration

Here are the configuration instructions for Laravel Framework. You’ve to edit the config/app.php file and append the following line to register the service provider:

'providers' => [
    // ...

 OwenIt\Auditing\AuditingServiceProvider::class,

    // ...
],

If you want to utilize this package in Lumen. you can view its documentation here.

Publishing

After configuring in Laravel, You’ve to use the following command to publish the configuration settings:

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

This will produce the config/audit.php configuration file.

Basic Usage

Model Setup

Next, you’ve to set up a model for auditing, You can do it by adding the OwenIt\Auditing\Auditable trait in the model that you wish to audit and implement the OwenIt\Auditing\Contracts\Auditable interface.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;

class User extends Model implements Auditable
{
    use \OwenIt\Auditing\Auditable;
    // ...
}

By default, the Database audit driver will be utilized. But you can also implement your own Audit Driver.

Getting Audits

After that, you’ve to retrieve audit records via Eloquent relations.

Fetching audit records

See the Article model example below:

// Get first available Article
$article = Article::first();

// Get all associated Audits
$all = $article->audits;

// Get first Audit
$first = $article->audits()->first();

// Get last Audit
$last = $article->audits()->latest()->first();

// Get Audit by id
$audit = $article->audits()->find(4);

Getting audit records with the associated User model

// Get all associated Audits
$all = $article->audits()->with('user')->get();

Getting the Audit metadata

Now you can retrieve an array with the Audit
metadata.

// Get first available Article
$article = Article::first();

// Get latest Audit
$audit = $article->audits()->latest()->first();

var_dump($audit->getMetadata());

And here is the output:

array(10) {
  ["audit_id"]=>
  string(1) "1"
  ["audit_event"]=>
  string(7) "updated"
  ["audit_url"]=>
  string(26) "http://example.com/articles/1"
  ["audit_ip_address"]=>
  string(9) "127.0.0.1"
  ["audit_user_agent"]=>
  string(68) "Mozilla/5.0 (X11; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
  ["audit_tags"]=>
  string(7) "foo,bar"
  ["audit_created_at"]=>
  string(19) "2017-01-01 01:02:03"
  ["audit_updated_at"]=>
  string(19) "2017-01-01 01:02:03"
  ["user_id"]=>
  string(1) "1"
  ["user_type"]=>
  string(8) "App\User"
}

Getting the modified properties

You can also fetch modified properties of the Auditable model in an array. The data includes the old and new values for each attribute.

// Get first available Article
$article = Article::first();

// Get latest Audit
$audit = $article->audits()->latest()->first();

var_dump($audit->getModified());

Here’s the Output:

array(2) {
  ["title"]=>
  array(2) {
    ["new"]=>
    string(28) "How To Audit Eloquent Models"
    ["old"]=>
    string(19) "How to audit models"
  }
  ["content"]=>
  array(2) {
    ["new"]=>
    string(62) "First, let's start by installing the laravel-auditing package."
    ["old"]=>
    string(16) "This is a draft."
  }
}

If you want to learn more about Laravel Auditing you can view its Official Documentation and Source Code on Github.

Published at : 09-08-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project