“我想在Laravel中执行基本的CRUD操作,我想根据product_id检索存储在数据库中的数据,并执行更新和删除相关数据等操作。但是,当我编写搜索查询时,如$cartItem = CartItem::where('product_id',$productId)->where('order_code',$orderCode)->first();,即使数据库中存在数据,它也会返回空。
class Store extends Controller {
public function __invoke($id)
{
try
{
$products = Products::findOrFail($id);
$quantity = rand(1,10);
if(!$products)
{
throw new \Exception("product not found");
}
$orderCode = session('order_code');
if(!$orderCode)
{
$orderCode = rand(100,2000);
session(['order_code' => $orderCode]);
}
$cartItem = CartItem::where('product_id', $productId)->where('order_code', $orderCode)>first();
if ($cartItem)
{
$cartItem->quantity+=$quantity;
$cartItem->total_price = $cartItem->unit_price * $cartItem->quantity;
$cartItem->save();
throw new \Exception('The product is available in the cart');
}
$cart_item = new CartItem([
"title"=>$products->title,
'quantity'=>$quantity,
'unit_price'=> $products->price,
'total_price'=>$products->price*$quantity,
'order_code' => $orderCode,
"product_id"=>$products->product_id,
]);
$cartSave = $cart_item->save();
if($cartSave){
throw new \Exception('product added to cart');
}
}
catch(\Exception $e)
{
\Log::debug($e->getMessage());
return Response::error($e->getMessage());
}
}
} 你的文字
1条答案
按热度按时间gopyfrb31#
你是否从
__invoke
方法中记录了$productId
和$orderCode
,以确保它们是从 AJAX 请求中到达的?使用XDebug单步执行该方法可能是一个更好的选择(如果可以的话),它允许您在过程的每一步都看到值。