Download the PHP package waska14/laravel-with-db-transactions without Composer
On this page you can find all versions of the php package waska14/laravel-with-db-transactions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download waska14/laravel-with-db-transactions
More information about waska14/laravel-with-db-transactions
Files in waska14/laravel-with-db-transactions
Package laravel-with-db-transactions
Short Description Use middleware globally for all routes to put every controller in database transaction
License MIT
Homepage https://github.com/waska14/laravel-with-db-transactions
Informations about the package laravel-with-db-transactions
waska14/laravel-with-db-transactions
Are you tired of writing database transactions for every controller method?
This package solves the problem with middleware.
Middleware begins database transaction and depending response (and config also) commits/rollbacks it.
Docs
- Installation
- Configuration (Config based management)
- Manual usage
- Actions and events
Installation
Add the package in your composer.json by executing the command.
For Laravel versions before 5.5 or if not using auto-discovery, register the service provider in config/app.php
Configuration
If you want to change default configuration, you must publish default configuration file to your project by running this command in console:
This command will copy file [/vendor/waska14/laravel-with-db-transactions/config/waska.with_db_transactions.php] to [/config/waska.with_db_transactions.php]
Default waska.with_db_transactions.php looks like:
You can manage route middleware globally (automatically) from config:
| Key | Value(s) | Comment |
|---|---|---|
| middleware_groups | Keys of protected $middlewareGroups from app/Http/Kernel.php. |
Every route having the group middleware will be processed with database transaction. |
| ignore_request_methods | HTTP request methods names (GET/HEAD) | Those methods won'be processed with database transaction. |
| ignore_route_names | route names | Those methods also won't be processed with database transaction. |
| commit_http_statuses | HTTP status codes | If transaction has begun and the response has any of those statuses, the transaction will be committed. |
| maximum_attempts | integer |
Maybe if deadlocks occurs or something else |
If you want manually management, you can use middleware_alias (or change it, also) as route middleware.
Manual usage
If you define middleware_groups as an empty array in config, none of the routes will be processed with database transactions by default, until you set manually the middleware for the specific routes/route groups.
Note that you can fill middleware_groups and also use middleware manually anywhere you want the method to be processed with database transaction.
Default middleware alias is with_db_transactions which can be changed in config, also.
Note that all configuration (ignore_route_names, ignore_request_methods, maximum_attempts, commit_http_statuses etc.) will also work when using this package manually.
Actions and Events
After version 2.0.0 you can define closures that will be executed before/after commit/rollback(s).
There are 6 static functions that you can call with Waska\LaravelWithDBTransactions\Helpers\WithDBTransactions class:
Usage:
Note that each function can be called as many times as you want. All closures will be executed with the same sequence. E.x:
After rollback storage/logs/laravel.log looks like:
After version 2.0.0 package dispatches events also.
You can listen them and do whatever you want in listener.
List of events:
\Waska\LaravelWithDBTransactions\Events\BeforeBeginTransactionEventBefore begin transaction\Waska\LaravelWithDBTransactions\Events\AfterBeginTransactionEventAfter begin transaction\Waska\LaravelWithDBTransactions\Events\BeforeCommitEventBefore commit\Waska\LaravelWithDBTransactions\Events\AfterCommitEventAfter commit\Waska\LaravelWithDBTransactions\Events\BeforeRollbackEventBefore latest rollback\Waska\LaravelWithDBTransactions\Events\AfterRollbackEventAfter latest rollback\Waska\LaravelWithDBTransactions\Events\BeforeEveryRollbackEventBefore every rollback\Waska\LaravelWithDBTransactions\Events\AfterEveryRollbackEventAfter every rollback