ItSolutionStuff.com

How to use DB Transactions in Laravel?

By Hardik Savani • April 16, 2024
PHP Laravel

Hello,

This article is focused on how to use db transaction in laravel. you will learn db transaction laravel example. This article goes in detailed on how use database transactions laravel. It's a simple example of laravel db transaction try catch. Let's see below example laravel eloquent database transactions.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

Sometimes, we need to store data in tables with table dependency. For example, i created users, users_profile and users_address tables. When I will create a new user then I just need to store data in the profile and address tables as well. But if there is any cause to adding wrong format data or if anything happens then it can be a problem to store data in the dependent table. However, laravel eloquent added database transactions, so whenever we have an error or issue then it will roll back the database entry.

Let's see the code of how to use db transaction in laravel.

Example Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;

use Exception;

use App\Models\User;

use App\Models\UserProfile;

use App\Models\UserAddress;

class UserController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function store(Request $request)

{

try {

/*------------------------------------------

--------------------------------------------

Start DB Transaction

--------------------------------------------

--------------------------------------------*/

DB::beginTransaction();

/* Create New User */

$user = User::create([

'name' => $request->name,

'email' => $request->email,

'password' => bcrypt($request->password)

]);

/* Create New User Profile */

$userProfile = UserProfile::create([

'user_id' => $user->id,

'birthdate' => $request->birthdate,

'gender' => $request->gender,

'phone' => $request->phone,

]);

/* Create New User Address */

$userAddress = UserAddress::create([

'user_id' => $user->id,

'address' => $request->address,

'city' => $request->city,

'state' => $request->state0,

]);

/*------------------------------------------

--------------------------------------------

Commit Transaction to Save Data to Database

--------------------------------------------

--------------------------------------------*/

DB::commit();

} catch (Exception $e) {

/*------------------------------------------

--------------------------------------------

Rollback Database Entry

--------------------------------------------

--------------------------------------------*/

DB::rollback();

throw $e;

}

}

}

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 9 Eloquent Mutators and Accessors Example

Read Now →

Laravel Eloquent without() and withOnly() Method Example

Read Now →

Laravel Eloquent Sum Multiple Columns Example

Read Now →

Laravel Eloquent updateOrCreate Example

Read Now →

Laravel Eloquent take() and skip() Query Example

Read Now →

How to Create and Use Query Scope in Laravel Eloquent

Read Now →

How to use Union Query with Laravel Eloquent?

Read Now →

Laravel Eloquent Relationships Tutorial From Scratch

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →

Laravel Has Many Through Eloquent Relationship Tutorial

Read Now →

Laravel Eloquent Where Like Query Example Tutorial

Read Now →

How to Get Query Log in Laravel Eloquent?

Read Now →