php 我想使用Laravel处理MongoDB中收集的数据以获得结果

rqenqsqc  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(142)

“我想在Laravel中执行基本的CRUD操作,我想根据product_id检索存储在数据库中的数据,并执行更新和删除相关数据等操作。但是,当我编写搜索查询时,如$cartItem = CartItem::where('product_id',$productId)->where('order_code',$orderCode)->first();,即使数据库中存在数据,它也会返回空。
class Store extends Controller {

  1. public function __invoke($id)
  2. {
  3. try
  4. {
  5. $products = Products::findOrFail($id);
  6. $quantity = rand(1,10);
  7. if(!$products)
  8. {
  9. throw new \Exception("product not found");
  10. }
  11. $orderCode = session('order_code');
  12. if(!$orderCode)
  13. {
  14. $orderCode = rand(100,2000);
  15. session(['order_code' => $orderCode]);
  16. }
  17. $cartItem = CartItem::where('product_id', $productId)->where('order_code', $orderCode)>first();
  18. if ($cartItem)
  19. {
  20. $cartItem->quantity+=$quantity;
  21. $cartItem->total_price = $cartItem->unit_price * $cartItem->quantity;
  22. $cartItem->save();
  23. throw new \Exception('The product is available in the cart');
  24. }
  25. $cart_item = new CartItem([
  26. "title"=>$products->title,
  27. 'quantity'=>$quantity,
  28. 'unit_price'=> $products->price,
  29. 'total_price'=>$products->price*$quantity,
  30. 'order_code' => $orderCode,
  31. "product_id"=>$products->product_id,
  32. ]);
  33. $cartSave = $cart_item->save();
  34. if($cartSave){
  35. throw new \Exception('product added to cart');
  36. }
  37. }
  38. catch(\Exception $e)
  39. {
  40. \Log::debug($e->getMessage());
  41. return Response::error($e->getMessage());
  42. }
  43. }

} 你的文字

gopyfrb3

gopyfrb31#

你是否从__invoke方法中记录了$productId$orderCode,以确保它们是从 AJAX 请求中到达的?使用XDebug单步执行该方法可能是一个更好的选择(如果可以的话),它允许您在过程的每一步都看到值。

相关问题