我正在嘗試添加到購物車按鈕,但此行中發生錯誤, if (Cart::where('prod_id', $product_id)->where('user_id', Auth::id()->exists())) {
錯誤是:預期型別“物件”。找到 'int|string|null'。它來自Auth::id我正在學習的課程,它沒有犯任何錯誤并且作業正常
class CartController extends Controller
{
public function addProduct(Request $request)
{
$product_id = $request->input('product_id');
$product_qty = $request->input('product_qty');
if (Auth::check()) {
$prod_check = Product::where('id', $product_id)->first();
if ($prod_check) {
if (Cart::where('prod_id', $product_id)->where('user_id', Auth::id()->exists())) {
return response()->json(['status' => $prod_check->name . ' Already Added to Cart']);
} else {
$cartitem = new Cart();
$cartitem->prod_id = $product_id;
$cartitem->user_id = Auth::id();
$cartitem->prod_qty = $product_qty;
$cartitem->save();
return response()->json(['status' => $prod_check->name . ' Added to Cart']);
}
}
} else return response()->json(['status' => 'Login to Continue']);
}
}
uj5u.com熱心網友回復:
您正在將括號中的括號混合在一起->where('user_id', Auth::id()->exists())。存在應該是 queryBuilder 的外側。之后你應該呼叫->where('user_id', Auth::id())你應該鏈接 ->exists()。
if (Cart::where('prod_id', $product_id)->where('user_id', Auth::id())->exists()) {
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428458.html
