Tracking User Activities using Laravel Audit Package

Hello artisan in this example i am going to discuss step by step about tracking change activities using laravel audit in laravel with source code. So in this Laravel laravel auditing example i will use owen-it/laravel-auditing package to check user activity.

We can check user activity using many packages. But in this example i am going to use owen-it/laravel-auditing package. It's realy cool and easy to use and handle. After practicing this tutorial you will learn who was creating the entity, updated the entity, and even deleted the entity. You will also learn what changes have been made, the exact time it occurred.

 

Step 1 : Install Package

In the first step we have to install our package. So open your terminal and run this below command

composer require owen-it/laravel-auditing

 

And generate and publish the vendor and the audits table migration by:

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

 

Now migrate the table

php artisan migrate

 

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

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

 

Now all are set to go. Let's say I have a Product model like this:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

 

Add update your Product model like this:

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 = [];
}

 

And that's it. Now time to check the audits table in the database and you should see the auditing rows for the product.

 

Audit Query
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

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js