Download the PHP package azmolla/transaction-middleware without Composer
On this page you can find all versions of the php package azmolla/transaction-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download azmolla/transaction-middleware
More information about azmolla/transaction-middleware
Files in azmolla/transaction-middleware
Package transaction-middleware
Short Description A Laravel middleware package that wraps DELETE requests in a DB transaction also a provides trait and class to call methods in a database transaction.
License MIT
Homepage https://github.com/AbiruzzamanMolla/transaction-middleware-laravel
Informations about the package transaction-middleware
Transaction Middleware for Laravel
Transaction Middleware for Laravel is a simple package that provides a middleware to wrap DELETE requests in a database transaction. This means that if an error occurs during a DELETE request, any database changes will be rolled back automatically, ensuring data integrity.
Version: 1.0.6
Features
-
DB Transaction on DELETE Requests: Wraps only DELETE method requests in a database transaction. If an exception is thrown during request processing, the transaction is rolled back.
-
Flexible Auto-application: Control how the middleware is applied via configuration:
auto_apply_global
— Apply automatically to global middleware.auto_apply_web
— Apply automatically to the web group only.auto_apply_api
— Apply automatically to the api group only.
-
Manual Attachment: Even if you choose not to auto-apply, the middleware is available via its alias (
transaction
) for manual attachment on routes. - Laravel Auto-Discovery: The package uses Laravel’s auto-discovery so you don’t need to manually register the service provider.
Requirements
- PHP ^8.0
- Laravel 9.x, 10.x, 11.x or 12.x (or higher)
Installation
Install via Composer:
Laravel will automatically discover the service provider. If you need to publish the configuration file, run:
This will copy the configuration file to your Laravel application’s config
directory as transaction-middleware.php
.
Configuration
The published config file (config/transaction-middleware.php
) looks like this:
How It Works
-
Auto-application Mode: If you set
auto_apply_web_api
totrue
, the service provider will automatically push the middleware to both the web and api middleware groups. -
Selective Auto-application: Alternatively, you can set only one of the other two options (
auto_apply_web
orauto_apply_api
) totrue
to have the middleware applied only to that particular group. - Manual Mode:
With all auto-apply options set to
false
, you must manually attach the middleware to your routes using the aliastransaction
.
Usage
1. Automatic Middleware Application
After publishing the config file, edit it as needed:
-
To add the middleware to both groups:
-
To add it only to the web group:
- To add it only to the API group:
In these modes, you don’t need to add the middleware manually; it will automatically be pushed into the appropriate middleware groups.
2. Manual Middleware Application
If you prefer to control where the middleware is applied, leave all auto-apply options as false
. Then, attach the middleware to your routes like so:
3. Transaction Trait
The easiest will be to use the \Azmolla\TransactionMiddleware\Traits\HasTransactionCalls
trait which adds a transaction()
method.
Transaction chained Method
You can call the transaction()
without any argument and the method after will be called in a transaction.
This example will call the delete()
method in a transaction.
This is useful if you have any listeners also running database queries, like deleting child models.
The transaction will prevent you from corrupted data if any of the queries fails.
Conditional Callback
If you want you can also pass a callback to the transaction()
method you will get the calling object as first argument.
How It Works Under the Hood
-
Middleware Logic: The middleware checks if the request is a DELETE method. If so, it starts a DB transaction using
DB::beginTransaction()
. The request is then passed along the middleware pipeline. If the request processing completes without error,DB::commit()
is called. In case of an exception,DB::rollBack()
is executed, and the exception is rethrown. -
Service Provider: The
TransactionMiddlewareServiceProvider
merges your package’s configuration with the application config, publishes the config file for customization, and conditionally pushes the middleware into the appropriate groups based on the configuration values. -
Laravel Auto-discovery: Thanks to the
extra
block in the composer.json, Laravel auto-discovers and registers the service provider, so there’s no manual configuration required inconfig/app.php
. - Trait Usage:
You can use the
\Azmolla\TransactionMiddleware\Traits\HasTransactionCalls
trait to add atransaction()
method to your models.
Contributing
Contributions are welcome! Feel free to fork the repository and open a pull request. Please follow the established coding standards and include tests for new features or bug fixes.
License
This package is open-sourced software licensed under the MIT license.
Changelog
Version 1.0.6
- Support for Laravel ^12
Version 1.0.5
- Add a trait and class to call methods in a database transaction.
Version 1.0.4
- update 'auto_apply_web_api' to 'auto_apply_global'
Version 1.0.3
- update readme.md
- update composer.json
Version 1.0.2
- update readme.md
Version 1.0.1
- updated composer.json
Version 1.0.0
- Initial release of Transaction Middleware for Laravel.
- Provides DB transaction wrapping for DELETE requests.
- Configurable auto-application to web and/or API middleware groups.
Happy coding and thanks for using Transaction Middleware for Laravel! If you have questions or run into issues, feel free to open an issue on GitHub.