DEV Community

Dendi Handian
Dendi Handian

Posted on • Updated on

Tracking Change Activities using Laravel-Audit

Auditing is a way to track your entity's changes in the database. We could tell who was creating the entity, updated the entity, and even deleted the entity. We also could tell what changes have been made, the exact time it occurred, what browser/user-agent the user was used, and the IP address.

Such a powerful tool, isn't it? But some users may not like it. So use auditing wisely and fairly. But anyway, let's implement it in a laravel app easily using laravel-auditing package.

Prerequisites to code along

Prepare your own existing or new ready-to-be-developed laravel app.

Installation and Setup

Install it using composer:

composer require owen-it/laravel-auditing
Enter fullscreen mode Exit fullscreen mode

and generate the audits table migration by:

php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="migrations"
Enter fullscreen mode Exit fullscreen mode

migrate the table immediately:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

and you might want to export the config file for the later adjustment by:

php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="config"
Enter fullscreen mode Exit fullscreen mode

Auditing a Model

Let's say I have a Product model like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $guarded = [];
}

Enter fullscreen mode Exit fullscreen mode

Add the auditing interface and trait to the Product model like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
use OwenIt\Auditing\Auditable as AuditableTrait;

class Product extends Model implements Auditable
{
    use AuditableTrait;

    protected $guarded = [];
}
Enter fullscreen mode Exit fullscreen mode

And that's it. Now you just perform some CRUD for the product by using the Product model, not by changing it directly using DB query or another model. Check the audits table in the database and you should see the auditing rows for the product.

SELECT * FROM `audits`


id  user_type   user_id event   auditable_type  auditable_id    old_values  new_values  url ip_address  user_agent  tags    created_at  updated_at  
1   App\Models\User 976 created App\Models\Product  1297    []  {"title":"Quisquam autem id ei","description":"Exc...   http://laravel-prep.test/en/products    127.0.0.1   Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb...   
    NULL
    2020-11-29 10:44:48 2020-11-29 10:44:48 
2   App\Models\User 976 created App\Models\Product  1298    []  {"title":"Ullamco laboriosam","description":"Repel...   http://laravel-prep.test/en/products    127.0.0.1   Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb...   
    NULL
    2020-11-29 10:44:57 2020-11-29 10:44:57 
3   App\Models\User 976 updated App\Models\Product  1296    {"title":"Quisquam autem id ei"}    {"title":"Voluptatibus sunt i"} http://laravel-prep.test/en/products/1296   127.0.0.1   Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb...   
    NULL
    2020-11-29 10:50:49 2020-11-29 10:50:49 
4   App\Models\User 976 deleted App\Models\Product  1297    {"id":1297,"title":"Quisquam autem id ei","descrip...   []  http://laravel-prep.test/en/products/1297   127.0.0.1   Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb...   
    NULL
    2020-11-29 10:51:14 2020-11-29 10:51:14 
Enter fullscreen mode Exit fullscreen mode

The next step for you maybe is creating a new page to show the audit list for the admin. That's should be easy. Have fun.


version used:

Laravel Framework 8.12.3
owen-it/laravel-auditing v11.0.0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)