Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 24, 2022 04:36 am GMT

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

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 a Product model like this:

namespace App\Models;use Illuminate\Database\Eloquent\Model;class Product extends Model{    protected $guarded = [];}

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

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 of laravel-auditing in a more indepth guide and manner here is a related link:

Laravel Auditing Package: Track all Your Model Changes


Original Link: https://dev.to/dalelantowork/laravel-8-audit-for-beginners-5-easy-steps-4k6c

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To