Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Laravel 8 - Audit for Beginners (5 easy steps) ✍🏻📒💾
DaleLanto
DaleLanto

Posted on • Edited on

     

Laravel 8 - Audit for Beginners (5 easy steps) ✍🏻📒💾

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.

Image description

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

Run Migrate:

php artisan migrate
Enter fullscreen modeExit fullscreen mode

Step 3: Export the config file for the later adjustment

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

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 = [];}
Enter fullscreen modeExit fullscreen mode

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 = [];}
Enter fullscreen modeExit 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_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
Enter fullscreen modeExit 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 oflaravel-auditing in a more indepth guide and manner here is a related link:

Laravel Auditing Package: Track all Your Model Changes

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
msnmongare profile image
Sospeter Mong'are
Software Engineer passionate about developing for the web
  • Location
    Kenya
  • Education
    BSC. Software Engineering
  • Pronouns
    Mr
  • Work
    Software 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.

CollapseExpand
 
msnmongare profile image
Sospeter Mong'are
Software Engineer passionate about developing for the web
  • Location
    Kenya
  • Education
    BSC. Software Engineering
  • Pronouns
    Mr
  • Work
    Software 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

CollapseExpand
 
adil_radidi profile image
ADIL RADIDI
Développeur Full Stack Junior spécialisé dans la pile Vue.js / Laravel
  • Location
    Casablanca,Morocco
  • Joined

Thank you, this is so important.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Your friendly neighborhood cat developer! 🐈👨🏻‍💻#bestpractices#php #laravel #reactjs
  • Location
    Manila, Philippines
  • Education
    Pamantasan ng Lungsod ng Maynila
  • Work
    Full Stack Developer at Crypto Based Community Platform
  • Joined

More fromDaleLanto

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp