PHP code example of elaborate-code / laravel-eloquent-logs

1. Go to this page and download the library: Download elaborate-code/laravel-eloquent-logs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elaborate-code / laravel-eloquent-logs example snippets


return [
    'logs_model' => \ElaborateCode\EloquentLogs\Models\EloquentLog::class,
    'logs_table' => 'eloquent_logs',

    'user' => \App\Models\User::class,
];

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ExampleModel extends Model
{
    use \ElaborateCode\EloquentLogs\Concerns\HasLogs;
    // ...
}

$example_model->eloquentLogs;

$example_model->load('eloquentLogs');

App\Models\ExampleModel::with('eloquentLogs')->find($id);

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

$example_model = ExampleModel::create(['name' => 'foo']);

$example_model->update(['name' => 'bar']);

$example_model->delete();

// ⚠️ This will result in 3 queries to insert the 3 events logs into the database  

use ElaborateCode\EloquentLogs\Facades\CacheEloquentLogQueries;

CacheEloquentLogQueries::start();

$example_model = ExampleModel::create(['name' => 'foo']);

$example_model->update(['name' => 'bar']);

$example_model->delete();

CacheEloquentLogQueries::execute();

// 👍 This will result in 1 query to insert the 3 events logs into the database  

// Stops caching and empties the cache without queries execution
CacheEloquentLogQueries::reset();

// Empties the cache but doesn't stop caching
CacheEloquentLogQueries::flushQueries();

// Stops caching until the reuse of start() and doesn't empty the cache
CacheEloquentLogQueries::suspend();

// Returns a boolean
CacheEloquentLogQueries::isCaching();

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ExampleModel extends Model
{
    use \ElaborateCode\EloquentLogs\Concerns\HasLogs;

    public static array $loggableOptions = [
        'ignore' => ['created', 'updated', 'deleted', 'softDeleted', 'forceDeleted', 'restored'],
    ];
    // ...
}

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 ...
    }
}

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

// Silent eloquent queries ...

\Illuminate\Database\Eloquent\Model::setEventDispatcher(app(Dispatcher::class));
// ...
bash
php artisan vendor:publish --tag="eloquent-logs-migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --tag="eloquent-logs-config"