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]);
});

IMPLEMENTING LARAVEL TRANSACTIONS

DELETING, UPDATING AND INSERTING All the users data to our database more safely.

https://miro.medium.com/max/755/1*-BReypAVx4QD7Ux5FWqRHA.png

Database Transaction Usage in Laravel 6

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

using The DB Facade

DB::transaction(function()
{
    // Laravel 6 Codes using Eloquent models and 
    // here some Query Builder
});

Example:

DB::transaction(function()
{
    $item = new Item();
    $item->name = 'Mobile Iphone';
    $item->price = 295800;
    $item->save();
 
    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();
 
            try
            {
                $item = new Item();
                $item->name = $input['name'];
                $item->description = $input['description'];
                $item->price = $input['price'];
        
                $vendor = Auth::user()->vendor()->get();
                $item->vendor()->associate($vendor[0]);
                $item->save();
                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 :

Summary

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 infinityknow.com 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.

Leave a Comment