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.

  Simple Model Popup Box using HTML CSS and JavaScript

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

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.


Download Project Scripts

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 :

  Laravel Combine two DB query results Example

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP Laravel 6 Multiple Database Connections Tutorial
  2.   Laravel Complex queries using Eloquent Query
  3.   Google Charts Change tooltip position
  4.   Laravel 5.8 Global Blade view variable
  5.   Query Scope using Laravel 6 Eloquent Example
  6.   laravel flash message Notification in Blade template
  7.   Vuejs Editable Grid component - GridView in Vue.JS
  8.   Laravel 5.7 Routing Optional Parameter
  9.   Laravel 6 Eloquent LEFT JOIN Query Example Tutorial
  10.   validation Update User Profile in Laravel 5.7

Leave a Reply

Your email address will not be published. Required fields are marked *