Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen transactions

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

like image 810
Riccoh Avatar asked Apr 02 '26 05:04

Riccoh


2 Answers

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
});
like image 137
Romantic Dev Avatar answered Apr 04 '26 05:04

Romantic Dev


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

like image 32
ahmad izhar Avatar answered Apr 04 '26 04:04

ahmad izhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!