PHP code example of dinhdjj / laravel-auto-db-transaction-middleware

1. Go to this page and download the library: Download dinhdjj/laravel-auto-db-transaction-middleware 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/ */

    

dinhdjj / laravel-auto-db-transaction-middleware example snippets


/**
 * The application's route middleware groups.
 *
 * @var  array
 */
protected $middlewareGroups = [
    'web' => [
        ...,
        \Dinhdjj\AutoDBTransaction\AutoDBTransactionMiddleware::class,
    ],

    'api' => [
        ...,
        \Dinhdjj\AutoDBTransaction\AutoDBTransactionMiddleware::class,
    ]

//App\Exceptions\Handler

$this->reportable(function (InvalidOrderException $e) {
    //
})->stop();

$this->reportable(function (InvalidOrderException $e) {
    return false;
});

namespace App\Exceptions;
 
use Exception;
 
class InvalidOrderException extends Exception
{
    /**
     * Report the exception.
     *
     * @return bool|null
     */
    public function report()
    {
        return true;
        // or
        return null;
    }
}

// 1. use your own db-transaction
DB::beginTransaction();
// your code
DB::rollBack();

// 2. You helper method to rollback the package's db-transaction
$this->reportable(function (InvalidOrderException $e) {
    \Dinhdjj\AutoDBTransaction\Facades::rollBack();
})->stop();