如果数量小于0,如何从购物车中删除商品。拉拉韦尔

时间:2019-02-22 06:04:43

标签: php laravel

我的问题是,当数量小于0且数量将为-1,-2时,该商品不会从购物车中移除。您能帮我吗?

这是我的控制人

 public function reducequantity(Request $request){
    $id = $_GET['id']; 
    $oldcart = Session::has('cart') ? Session::get('cart') : null;
    $cart = new Cart($oldcart);
    $cart->reducequantity($id);
    Session::put('cart',$cart);      
    echo json_encode("item reduced from cart");
    }

这是我的模型

    public function reducequantity($id){       
        $this->items[$id]['qty']--;
        $this->items[$id]['price'] -=  $this->items[$id]['item']['price'];
        $this->totalqty--;
        $this->total_price -=  $this->items[$id]['item']['price'];
        if($this->items[$id]['qty'] <= 0){ 
            unset($this->items[$id]);
        }       
   } 

1 个答案:

答案 0 :(得分:0)

    **Solved By making a little change in controller**
    public function removeitem(Request $request){
     $id = $_GET['id']; 
     $oldcart = Session::has('cart') ? Session::get('cart') : null;
     $cart = new Cart($oldcart);
      $cart->removeitem($id);
       if(count($cart->items) > 0){
         Session::put('cart',$cart);
     }else{
        Session::forget('cart');
     }

      echo json_encode("item removed");
}