I'am struggeling to get the transactions at work with laravel lumen, but still no success. I see alot of DB facade for the transactions, but is there a way that I can use something like this?
There is no clear documentation about transactions in Lumen.
app('db')->transaction(function() {
// DB work
});
Thanks in advance
We can use the closure approach to using transactions. If the closure throws an exception it will automatically be rolled back. if closure pass it will be committed. hope this helps.
app('db')->transaction(function() {
// DB work
});
yes, here is the proper way to perform you Db transactions in Lumen.
DB::beginTransaction();
try
{
DB::commit();
}
catch (\Exception $e)
{
DB::rollback();
}`
in try before DB::commit(); you can add your code . once all good it will save to db and if any exception occurs it will rollback and will not save data. Enjoy ! Happy Coding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With