Blog Detail


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.


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

composer require owen-it/laravel-auditing


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' => [
    // ...


    // ...

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


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.

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

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

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


And here is the output:

array(10) {
  string(1) "1"
  string(7) "updated"
  string(26) ""
  string(9) ""
  string(68) "Mozilla/5.0 (X11; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"
  string(7) "foo,bar"
  string(19) "2017-01-01 01:02:03"
  string(19) "2017-01-01 01:02:03"
  string(1) "1"
  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();


Here’s the Output:

array(2) {
  array(2) {
    string(28) "How To Audit Eloquent Models"
    string(19) "How to audit models"
  array(2) {
    string(62) "First, let's start by installing the laravel-auditing package."
    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
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