1. Go to this page and download the library: Download gazugafan/laravel-changelog 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/ */
Schema::table('widgets', function (Blueprint $table) {
$table->unsignedInteger('change_id')->nullable();
});
class Widget extends Model
{
use Changelog; //add all the changelog features
}
class Widget extends Model
{
use Changelog; //add all the changelog features
protected $forceChangelogging = true; //set to false to allow saving outside of changes
protected $changeIDColumn = 'change_id'; //in case you want to use a different column for some reason
}
Change::begin('Web API', 'Painting a widget red');
Change::transaction(function (){
$widget = \App\Widget::find(123);
$widget->color = 'red';
$widget->save();
$redPaint = \App\Paint::where('color', 'red')->first();
$redPaint->available -= 1;
$redPaint->save();
}, 'Web API', 'Painting a widget red', 5); //make 5 attempts in case of deadlock
try {
Change::begin('Web API', 'Painting a widget red', FALSE); //start a change without transactions
$widget = \App\Widget::find(123);
$widget->color = 'red';
$widget->save();
$redPaint = \App\Paint::where('color', 'derek')->first();
$redPaint->available -= 1; //there's no "derek" color, dummy! This is gonna blow up!
$redPaint->save();
Change::commit();
} catch (Exception $e) {
//great, now we've somehow painted a widget red without using any red paint.
//if only we had used transactions, we'd rollback painting the widget red here...
Change::rollBack(); //but this will still log the change's status as "failed", at least
}