DEV Community

Cover image for Laravel 8 - Audit for Beginners (5 easy steps) โœ๐Ÿป๐Ÿ“’๐Ÿ’พ
DaleLanto
DaleLanto

Posted on • Updated on

Laravel 8 - Audit for Beginners (5 easy steps) โœ๐Ÿป๐Ÿ“’๐Ÿ’พ

Laravel Auditing is a Laravel package that aims to make it easy to track 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 the User Agent, audit URL, and the IP address of the user. One of the main use-cases of the package is looking at suspicious activities or unexpected changes in the model.

Image description

You can read more about it here.

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
Enter fullscreen mode Exit fullscreen mode

Step 2: Generate and publish the vendor and the audits table migration by:

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

Run Migrate:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Step 3: Export the config file for the later adjustment

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

Step 4: Apply in the Model

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

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
    protected $guarded = [];
}
Enter fullscreen mode Exit fullscreen mode

Add update to 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 = [];
}
Enter fullscreen mode Exit fullscreen mode

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_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

Hurray! we have now applied, run and tested Laravel Auditing in 5 easy steps. Hope this helped!

Image description

To continue making use of laravel-auditing in a more indepth guide and manner here is a related link:

Laravel Auditing Package: Track all Your Model Changes

Discussion (0)