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.
Hope it can help you.
#laravel #packages #laravel-8x #user-activity-laravel