public function addToCart($userId, $productId) { $item = ShoppingCartItems::where('shopping_cart_id', $this->id)->where('product_id', $productId)->first(); if (!$item) { $item = new ShoppingCartItems(); $item->shopping_cart_id = $this->id; $item->product_id = $productId; $item->quantity = 1; $item->save(); } return $this->getItemCount($userId); }