/** * Function to add items to a cart */ public function addToCart($productid, $price, $quantity) { // check if the product already exists in the cart. // if it does, add the quantities together $productAlreadyExists = 0; $olderProduct = ShoppingCartItem::where('product_id', '=', $productid)->where('user_id', '=', Auth::user()->id)->first(); if ($olderProduct != null) { $productAlreadyExists = $olderProduct->qty; $olderProduct->qty += $quantity; $olderProduct->save(); } else { $item = new ShoppingCartItem(); $item->user_id = Auth::user()->id; $item->product_id = $productid; $item->price = $price; $item->qty = $quantity; $item->save(); } }