
Laravel Auditing is a Laravel package that aims to make it easy totrack eloquent model changes. The documentation describes Laravel Auditing as follows:
Laravel Auditing allows you to keep a history of model changes by simply using a trait. Retrieving the audited data is straightforward, making it possible to display it in various ways.
Along with model changes, each audit record contains theUser Agent
,audit URL
, and theIP address
of the user. One of the main use-cases of the package is looking at suspicious activities or unexpected changes in the model.
You can read more about ithere.
Now that you have an understanding of Laravel Auditing lets dive right in the codes!
Step 1: Install the 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
Step 2: Generate and publish the vendor and the audits table migration by:
php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="migrations"
Run Migrate:
php artisan migrate
Step 3: Export the config file for the later adjustment
php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="config"
Step 4: Apply in the Model
Let's say I have aProduct
model like this:
namespace App\Models;use Illuminate\Database\Eloquent\Model;class Product extends Model{ protected $guarded = [];}
Add update to yourProduct
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.
Step 5: 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_at1 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:482 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:573 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:494 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
Hurray! we have now applied, run and tested Laravel Auditing in 5 easy steps. Hope this helped!
To continue making use oflaravel-auditing
in a more indepth guide and manner here is a related link:
Top comments(3)

- LocationKenya
- EducationBSC. Software Engineering
- PronounsMr
- WorkSoftware Engineer (Backend) | API Development & Integration | Technical writer | Mentor
- Joined
Thank you, this is so important.
Just a question: what the difference between auditing is using this and logging using spartie package.

- LocationKenya
- EducationBSC. Software Engineering
- PronounsMr
- WorkSoftware Engineer (Backend) | API Development & Integration | Technical writer | Mentor
- Joined
They are all similar. they collect logs that happen when a change happens in the model

- LocationCasablanca,Morocco
- Joined
Thank you, this is so important.
For further actions, you may consider blocking this person and/orreporting abuse