1DB::beginTransaction();
2
3try {
4 DB::insert(...);
5 DB::insert(...);
6 DB::insert(...);
7
8 DB::commit();
9 // all good
10} catch (\Exception $e) {
11 DB::rollback();
12 // something went wrong
13}
1use Illuminate\Support\Facades\DB;
2
3DB::transaction(function () {
4 DB::update('update users set votes = 1');
5
6 DB::delete('delete from posts');
7});
1// try...catch
2try {
3 // Transaction
4 $exception = DB::transaction(function() {
5
6 // Do your SQL here
7
8 });
9
10 if(is_null($exception)) {
11 return true;
12 } else {
13 throw new Exception;
14 }
15
16}
17catch(Exception $e) {
18 return false;
19}
20
1DB::beginTransaction();
2
3try {
4 DB::insert(...);
5 DB::insert(...);
6 DB::insert(...);
7
8 DB::commit();
9 // all good
10} catch (\Exception $e) {
11 DB::rollback();
12 // something went wrong
13}
14
1DB::beginTransaction();
2
3try {
4 DB::insert(...);
5 DB::commit();
6} catch (\Throwable $e) {
7 DB::rollback();
8 throw $e;
9}
1DB::beginTransaction();
2try { /** Statement */ DB::commit(); }
3catch (\Exception $e) { /** Statement if failed */ DB::rollback(); }