Laravel 6 try catch Database Transaction

Today, We want to share with you Laravel 6 try catch Database Transaction.In this post we will show you Laravel: Using try…catch with DB::transaction(), hear for How to use Try/catch and database transaction in PHP and laravel we will give you demo and example for implement.In this post, we will learn about Fix exception handling on transaction commit in DB::transaction() with an example.

Laravel 6 try catch Database Transaction

There are the Following The simple About Laravel 6 Middleware for Database Transactions Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel database transactions with eloquent, so the Database Transaction Usage in Laravel 6 is used for this example is following below.

How to use DB transactions in laravel 6?

Example: 1

DB::transaction(function () {
    $product_id = DB::table('products')->insertGetId(['title' => 'Mobile']);
    DB::table('products_city')->insert(['product_id'=>$product_id,'name' => 'Rajkot']);
    DB::table('products_count')->insert(['product_id'=>$product_id,'price' => 25874]);


DELETING, UPDATING AND INSERTING All the users data to our database more safely.*-BReypAVx4QD7Ux5FWqRHA.png

Database Transaction Usage in Laravel 6

Laravel How to use Database Transaction with Eloquent ORM and Query Builder

using The DB Facade

    // Laravel 6 Codes using Eloquent models and 
    // here some Query Builder


    $item = new Item();
    $item->name = 'Mobile Iphone';
    $item->price = 295800;
    DB::table('products')->where('status', '=', 1)->delete();

Return Value from Transaction

    public function store(Request $request)
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'description' => 'required',
            'price' => 'required|integer|max:999999|min:0'
        if ($validator->fails()) 
            return response()->json(['error'=>$validator->errors()], 401);            
        $item_created = function() use ($request)
            $input = $request->all();
                $item = new Item();
                $item->name = $input['name'];
                $item->description = $input['description'];
                $item->price = $input['price'];
                $vendor = Auth::user()->vendor()->get();
                return response()->json(['success'=>json_decode ("{}")], $this->successStatus);
            catch(Exception $e)
                return response()->json(['error'=>'Sorry, Unable to create'], 400);
        $return = DB::transaction($item_created);
        return $return;

Web Programming Tutorials Example with Demo

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel eloquent begin transaction.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel Combine two DB query results Example
  2.   Laravel 6 Update Query using Eloquent Example
  3.   Add Word Count to Single Posts in WordPress
  4.   Laravel MySQL Advanced Query Techniques with Example
  5.   Laravel Multiple Authentication Example
  6.   How to use multiple databases in Laravel
  7.   Laravel if statement multiple conditions - if condition in Laravel
  8.   How to use multiple databases in Laravel
  9.   Laravel 6 Eloquent LEFT JOIN Query Example Tutorial
  10.   Laravel Multiple Database Connections Step By Step