我正在创建一个 Laravel 电子商务网站,并且正在使用 Gloudemans\Shoppingcart\ 库。我在使用“添加到购物车”按钮时遇到问题。我再解释一下!我有一个 singleProduct.blade.php 文件,它从控制器调用产品信息:
这是将数据从数据库传递到视图的函数:
public function show($name)
{
$iamlush = iamlush::where('name', $name)->firstOrFail();
return view('singleProduct')->with('product', $iamlush);
}
这是单个产品文件:
<div class="contentContainer">
<div class="rowContainer text-center">
<div class="productImgContainer">
<img src="{{ asset('img/iamlush/shop/'.$product->img) }}" class="productImg">
</div>
<div class="productInfoContainer">
<div class="text-center">
<div class="productLogoContainer">
<img src="{{ asset('img/logo/'.$product->productLogo) }}" class="logoContainer">
</div>
<h3>{{ $product->name }}</h3>
<h5>{{ $product->priceFormat() }}</h5>
</div>
<p class="descriptionContainer">
{{ $product->description }}
</p>
<div class="rowContainer">
<div class="iconContainer">
<img src="{{ asset('img/productPage/productInfo.png') }}" class="icons">
</div>
<div class="iconContainer">
<img src="{{ asset('img/productPage/paymentMethods.jpg') }}" class="icons">
</div>
</div>
<form action="{{ route('cart.store') }}" method="POST" class="formContainer">
@csrf
<input type="hidden" name="id" value="{{ $product->id }}">
<input type="hidden" name="name" value="{{ $product->name }}">
<input type="hidden" name="price" value="{{ $product->price }}">
<div class="BtnContainer">
<button type="submit" class="btnCart">Add To Cart</button>
</div>
</form>
</div>
</div>
</div>
“添加到购物车”向“cart.store”发送 POST 请求,这是 web.php:
Route::post('/cart', 'CartController@store')->name('cart.store');
慕莱坞森