iamarunp/laraveldbtransactions

The package can be used to handle DB transaction through a middleware.

dev-master 2020-02-10 08:45 UTC

This package is auto-updated.

Last update: 2025-06-10 20:31:51 UTC


README

This package introduces the simplest way, you can achieve the database transaction without making your code complex, without making a lot of changes.the middleware can be used for individual routes or for a route group ,if an exception occurs then it will rollback from the Terminable Middleware. While if queries getting executed successfully then the transaction gets committed. In Laravel, DB facades provide the transaction for both query builder as well as eloquent ORM.

Installation

        composer require iamarunp/laraveldbtransactions

The terminate method will be automatically called after the response is ready to be sent to the browser.

The middleware is registered using TransactionServiceProvider, to use the provider, you need register the service provider.

Laravel 5.4 and below

To register your service provider, you just need to add an entry to the array of service providers in the config/app.php file.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 'providers' => [            /*          * Laravel Framework Service Providers...          */         Illuminate\Auth\AuthServiceProvider::class,         Illuminate\Broadcasting\BroadcastServiceProvider::class,         Illuminate\Bus\BusServiceProvider::class,         Illuminate\Cache\CacheServiceProvider::class,         Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,         Illuminate\Cookie\CookieServiceProvider::class,         Illuminate\Database\DatabaseServiceProvider::class,         Illuminate\Encryption\EncryptionServiceProvider::class,         Illuminate\Filesystem\FilesystemServiceProvider::class,         Illuminate\Foundation\Providers\FoundationServiceProvider::class,         Illuminate\Hashing\HashServiceProvider::class,         Illuminate\Mail\MailServiceProvider::class,         Illuminate\Notifications\NotificationServiceProvider::class,         Illuminate\Pagination\PaginationServiceProvider::class,         Illuminate\Pipeline\PipelineServiceProvider::class,         Illuminate\Queue\QueueServiceProvider::class,         Illuminate\Redis\RedisServiceProvider::class,         Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,         Illuminate\Session\SessionServiceProvider::class,         Illuminate\Translation\TranslationServiceProvider::class,         Illuminate\Validation\ValidationServiceProvider::class,         Illuminate\View\ViewServiceProvider::class,            /*          * Package Service Providers...          */         Laravel\Tinker\TinkerServiceProvider::class,            /*          * Application Service Providers...          */         App\Providers\AppServiceProvider::class,         App\Providers\AuthServiceProvider::class,         // App\Providers\BroadcastServiceProvider::class,         App\Providers\EventServiceProvider::class,         App\Providers\RouteServiceProvider::class,          iamarunp\Laraveldbtransactions\TransactionServiceProvider::, class,
],

Laravel 5.5+

If you're using Laravel 5.5 or above, the package will automatically registered.

Assigning Middleware to Routes in Laravel

Using Route Group

Route::group(['middleware' => 'TransactionHandler'], function () {
Route::get('/home', [
    'as' => 'home',
    'uses' => 'Dashboard\DashboardController@dashboard'
]);  

Route::resource('users','UserController'); 

// more route definitions

});

Using chain method

Route::get('/', function () {
        //
})->middleware(['TransactionHandler', 'second']);