Download the PHP package niclas-van-eyk/laravel-transactional-controllers without Composer
On this page you can find all versions of the php package niclas-van-eyk/laravel-transactional-controllers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download niclas-van-eyk/laravel-transactional-controllers
More information about niclas-van-eyk/laravel-transactional-controllers
Files in niclas-van-eyk/laravel-transactional-controllers
Package laravel-transactional-controllers
Short Description Effortlessly wrap your controller actions with database transactions.
License MIT
Homepage https://github.com/niclas-van-eyk/laravel-transactional-controllers
Informations about the package laravel-transactional-controllers
#[Transactional]
Laravel Controllers
Effortlessly wrap your controller actions with database transactions.
Installation
You can install the package via composer:
Background
If you want to make a series of edits to your database, where either all should happen at once, or none at all, you typically use database transactions. The example we use here is a user ($author
) transferring a certain $amount
of to another user ($receiver
). We also want to save that the fact that this transfer took place in a separate model (TransferLog
).
Usage
Before you might have written something like this:
You have to wrap your whole code inside one big closure, explicitly use
all parameters you inject, and if you want to return something from inside the transaction closure, you end up with this double return, making the code harder to read and your IDE angry.
laravel-transactional-controllers
solves this, by eliminating the need to wrap the code inside a closure and instead adding the Transactional
attribute to the controller method:
No more use
, double return
s or your IDE complaining about it not being able to guarantee a correct return type!
You can also explicitly specify the database connection to use for running the transaction (config('database.default')
is used by default):
Limitations
This only works when using controllers:
Implementation Details
This package uses Laravels ControllerDispatcher
component, which determines how the controller action should be executed. This means we can defer opening a transaction until the last possible moment, preventing unnecessary transactions from being opened! If e.g. the validation inside a FormRequest
fails, or a model is not found when using route model binding, no transaction is started.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
If you have any ideas for changes, feel free to open issues, PRs or fork the project.
Local Development
This assumes you already have installed sqlite, PHP, and all composer dependencies locally.
Run tests
Run formatter
Run analysis
Run all of the above at once
Credits
- Niclas van Eyk
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-transactional-controllers with dependencies
illuminate/contracts Version ^9.0 || ^10.0
illuminate/database Version ^9.0 || ^10.0
illuminate/routing Version ^9.5 || ^10.0