【发布时间】:2019-08-28 02:46:10
【问题描述】:
我有一个订单按钮,可以将用户重定向到包含已订购商品的购物车页面
<p class="btn-holder"><a href="{{route('addCart',$food->id) }}" class="btn btn-primary btn-block text-center" role="button" > Order this</a> </p>
这是web.php上的路由
Route::get('add-to-cart/{id}', 'FoodsController@addToCart')->name('addCart');
这是addToCart函数
public function addToCart($id){
$food = Food::find($id);
if(!$food) {
abort(404);
}
$cart = session()->get('cart');
// if cart is empty then this the first product
if(!$cart) {
$cart = [
$id => [
// "productId" => $food->id,
"name" => $food->food_item,
"quantity" => 1,
"price" => $food->price,
]
];
session()->put('cart', $cart);
return redirect()->back()->with('success', 'Product added to cart successfully!');
}
// if cart not empty then check if this product exist then increment quantity
if(isset($cart[$id])) {
$cart[$id]['quantity']++;
session()->put('cart', $cart);
return redirect()->back()->with('success', 'Product added to cart successfully!');
}
// if item not exist in cart then add to cart with quantity = 1
$cart[$id] = [
// "productId" => $food->id,
"name" => $food->food_item,
"quantity" => 1,
"price" => $food->price,
];
session()->put('cart', $cart);
return redirect()->back()->with('success', 'Product added to cart successfully!');
}
但是当我单击按钮时,它不会重定向到购物车页面,它会一直加载到同一个位置 我做了
dd($food);
在函数上,它输出正确的结果
【问题讨论】:
标签: laravel