/** * Run the database seeds. * * @return void */ public function run() { for ($i = 1; $i < 10; $i++) { \App\Models\Cart::create(['table_id' => $i, 'status' => 'active']); } $faker = Faker\Factory::create(); for ($i = 1; $i < 10; $i++) { for ($k = 1; $k < 6; $k++) { \App\Models\CartItem::create(['cart_id' => $i, 'product_id' => $k, 'quantity' => $faker->numberBetween(5, 20), 'status' => 'active']); } } }
/** * @param Request $request * @param $cartId * @return \Symfony\Component\HttpFoundation\Response */ public function addItemToCart(Request $request, $cartId) { $productId = $request->get('product_id'); $qty = $request->get('quantity'); // Check if the product is already in the cart $item = $this->checkAndUpdateProductInCart($cartId, $productId, $qty); if ($item) { return $this->responseOk($item); } // Get product price $price = $this->getProductPrice($productId); // Create new item $data = ['cart_id' => $cartId, 'product_id' => $productId, 'quantity' => $qty, 'unit_price' => $price, 'status' => 'pending']; $item = CartItem::create($data); return response()->json($item); }