Home > database >  how to condition same time product to insert and update in laravel
how to condition same time product to insert and update in laravel

Time:02-10

I am tring Order update.I need to update multi product in different row. when I update the product then it only updated created fields. i want to update current row and then new product insert in different row.

$product_id = $request->product_id;
$seller_id = Auth::user()->id;
$price = $request->product_price;

$quantity = $request->product_qty;
$payment_status = $request->payment_status;
$delivery_status = $request->delivery_status;
$shipping_type = "home_delivery";
$shipping_charge = $request->shipping_cost;
    
$user = Address::all()->where('user_id', $request->user_id)->first();
$cname= DB::table('users')->where('id', $user->user_id)->value('name');

$shipping_address = [

    'user_id'=>$request->user_id,
    'name'=> $request->name,
    'phone'=>$request->phone,
    'email'=>$user->email,
    'city'=>$user->city,
    'postal_code'=>$user->postal_code,
    'country'=>$user->country,
    'address'=>$request->address,
    
];

$order_id=$request->id;
$order =  Order::findOrFail($order_id)->update([

    'seller_id' => Auth::user()->id,
    'shipping_address' => json_encode($shipping_address),     
    'payment_status' => $request->payment_status,
    'payment_type' =>  $request->payment_option,
    'payment_details' => $request->payment_option,
    'coupon_discount' => $request->coupon_discount,
    'round_amount' => $request->round_discount,
    'grand_total' => $request->grand_total,
]);

for($i = 0; $i <= count($request->product_id)-1; $i  ){

    $data = [
        'order_id' => $order_id,
        'product_id' => $product_id[$i],
        'seller_id' => $seller_id,
        'price' => $price[$i],
        'quantity' => $quantity[$i],
        'payment_status' => $payment_status,
        'delivery_status' => $delivery_status,
        'shipping_type' => $shipping_type,
        'shipping_cost' => $shipping_charge,
    ];

    if(!empty(OrderDetail::where('order_id', $order_id))){
        OrderDetail::where('order_id', $order_id)->update($data);
    }
    else{
        Orderdetail::insert($data);
    }

    $product_stock_info = ProductStock::where('product_id', $product_id[$i])->first();
    // dd($product_id[$i]);
    $stock = $product_stock_info->qty;
    $updateStock = $stock - $quantity[$i];
    $product_stock_info->qty = $updateStock;
    $product_stock_info->save();   
}

my problem is here

if(!empty(OrderDetail::where('order_id', $order_id))){
  OrderDetail::where('order_id', $order_id)->update($data); 
} else {
  Orderdetail::insert($data);
}

CodePudding user response:

You can use updateOrCreate method .

the updateOrCreate method persists the model, so there's no need to manually call the save method

$flight = Flight::updateOrCreate(
        ['departure' => 'Oakland', 'destination' => 'San Diego'],
        ['price' => 99, 'discounted' => 1]
    );

as like

$whereData = [
              'order_id' => $order_id
             ];
OrderDetail::updateOrCreate($whereData ,$data); 
  • Related