Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1.  public function update(Order $order)
  2.     {
  3.         $attributes = $this->validateOrder();
  4.  
  5.         $order->update($attributes);
  6.  
  7.         $products = \request('products');
  8.         $quantity = \request('quantity');
  9.         $price = \request('price');
  10.         $discount = \request('discount');
  11.  
  12.         $total = 0;
  13.  
  14.         $previousPivot = DB::table('order_product')->where('order_id', $order->id)->get();
  15.  
  16.         // Check if products exists
  17.         if($products) {
  18.  
  19.             $order->products()->detach();
  20.  
  21.             foreach($products as $key => $value) {
  22.                 $attributes = [
  23.                     'price' => $price[$key],
  24.                     'quantity' => $quantity[$key],
  25.                     'discount' => $discount[$key],
  26.                 ];
  27.  
  28.                 $order->products()->attach($value, $attributes);
  29.  
  30.                 $toUpdateProduct = Product::findOrFail($value);
  31.  
  32.                 $previousPivotProduct = $previousPivot->where('product_id', 2);
  33.  
  34.                 if($previousPivotProduct) {
  35.                     dd($previousPivotProduct->first()->quantity);
  36.  
  37.                     $previousQty = $previousPivotProduct->first()->quantity;
  38.  
  39.                     dd($previousQty);
  40.                 }
  41.  
  42.             }
  43.  
  44.         }
  45.  
  46.         $this->flashMessage('success', 'Your order was updated with success!');
  47.  
  48.         return redirect()->back();
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement