Blog Detail

01

Aug
Easily Log Changes that Occur on Eloquent Models in Laravel cover image

arrow_back Easily Log Changes that Occur on Eloquent Models in Laravel

Laravel-eloquent-logs is a fantastic package that can log what happens to your Eloquent models (created|updated|deleted|soft deleted|restored|force deleted) and keep an eye on who made the change, how, and when. This solution is simple to integrate and introduces minimal changes to your project: 1 migration, 1 model, and 1 trait.

Installation

Install the package via composer:

composer require elaborate-code/laravel-eloquent-logs

Publish the migrations:

php artisan vendor:publish --tag="eloquent-logs-migrations"

Run the migrations:

php artisan migrate

Publishing config file [Optional]

You can publish the config file with:

php artisan vendor:publish --tag="eloquent-logs-config"

Usage

Pick an Eloquent model that you want to log the changes that happen to it and add the HasLogs trait to it.

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Example extends Model
{
    use HasFactory, \ElaborateCode\EloquentLogs\Concerns\HasLogs; // Changed
    // ...
}

After adding that trait, every change made to the model will be recorded.

Retrieving logs

You can load a model’s logs using the eloquentLogs relationship:

$example->eloquentLogs;

$example->load('eloquentLogs');

App\Models\Example::with('eloquentLogs')->find($id);
And you can query logs directly:

// latest 5 logs with affected models
ElaborateCode\EloquentLogs\Models\EloquentLog::with('loggable')->latest()->limit(5)->get()

And you can query logs directly:

// latest 5 logs with affected models
ElaborateCode\EloquentLogs\Models\EloquentLog::with('loggable')->latest()->limit(5)->get()

Muting Eloquent events [Laravel stuff]

From seeders:

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents; // Add this trait

    public function run(): void
    {
        // Silent eloquent queries ...
    }
}

Anywhere from your code:

\Illuminate\Database\Eloquent\Model::unsetEventDispatcher();

// Silent eloquent queries ...

\Illuminate\Database\Eloquent\Model::setEventDispatcher(app(Dispatcher::class));
// ...

For more details, you can visit Github.

Closing Notes

Being a custom web app development company, Codebrisk has a lot of experience developing CRM software, and e-commerce and SASS web applications for various industries. So if you want to build any web application, We are here to assist you with this initiative. Please feel free to get in touch with us or you can get a free quote here.

Published at : 01-08-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project