Spread the love

Database transactions in laravel eloquent are used to execute multiple or single database operations which maintainer ACID(atomicity, consistency, isolation, and durability) property of transactions.

In laravel we can archive the database transaction by DB facade. Laravel DB facade provides two methods to handle the transactions one through DB::transaction() method which handle all operations automatically and another manually registering the transaction methods beginTransaction, commit and rollout method.

Let’s understand the database transaction in laravel by example

Automatic transaction in laravel

Automatic transaction can be archive by DB::transaction() method of DB facade. we can run multiple database operation in closure function, if an exceptions is thrown then it will rollback the operation and if all operations executed successfully then it will commit the operations.

Example without transaction

// Create User Account
$user = User::create([
    'name' => Input::get('name'),
    'email' => Input::get('email'),
    'password' => Input::get('password'),
]);

// Create User Profile
$newUser = UserProfile::create([
    'dob' => Input::get('dob'),
    'user_id' => $user->id,
]);

// Create User Profile
$newUser = UserAddress::create([
    'address' => Input::get('address'),
    'city' => Input::get('city'),
    'state' => Input::get('state'),

]);

In the above example if operation UserProfile::Create get failed then there will be no entry for User profile and user address, and data will be not consistent in database. so to resolve the issue we will use database transaction in laravel eloquent as below

\DB::transactions(function(){
  // Create User Account
  $user = User::create([
     'name' => Input::get('name'),
    'email' => Input::get('email'),
    'password' => Input::get('password'),
  ]);

// Create User Profile
  $profile = UserProfile::create([
    'dob' => Input::get('dob'),
    'user_id' => $user->id,
  ]);

  // Create User Profile
  $newUser = UserAddress::create([
    'address' => Input::get('address'),
    'city' => Input::get('city'),
    'state' => Input::get('state'),
     'user_id' => $user->id,
   ]);
});

Here we used \DB::transactions to maintain the ACID property and it will execute and commit all the operation in the database.

Manual transaction in laravel

Same as Automatic transaction we can archive the transaction commit and rollback manually. We will use beginTransaction to start the transaction, commit to commit the changes in database and rollback to undo all changes in database transaction in laravel eloquent as below

\DB::beginTransaction();
try{
  
  // Create User Account
  $user = User::create([
     'name' => Input::get('name'),
    'email' => Input::get('email'),
    'password' => Input::get('password'),
  ]);

// Create User Profile
  $profile = UserProfile::create([
    'dob' => Input::get('dob'),
    'user_id' => $user->id,
  ]);

  // Create User Profile
  $newUser = UserAddress::create([
    'address' => Input::get('address'),
    'city' => Input::get('city'),
    'state' => Input::get('state'),
     'user_id' => $user->id,
   ]);
   \DB::commit();
}
catch(\Exception $e){
  \DB::rollback();
  throw $e;
}

Here we started the transaction in try catch block to caught the exception and rollback the operations.

Also Read : How to print database query in laravel 8 ?

Leave a Reply