表格拒绝添加第一次进入购物车,并从Laravel中的购物车中删除第一次进入
将Laravel项目从Github重新载入我的新电脑后,我得到了一个以前没有的新错误。我的购物车和产品页面的第一个条目无法通过给出的分配路线,但其余条目效果很好。在其他问题中,我真的找不到任何类似于我的问题的东西。
我试图用route :: post
的route :: match(['get','post']
)的,该>错误,结果我无法
在交换前后添加第一个条目:
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::post('/login/authenticate', [LoginController::class, 'authenticate'])->name('auth');
Route::get('/home', [PageController::class, 'home'])->name('home')->middleware('checklogin');
Route::get('/products', [PageController::class, 'products'])->name('product')->middleware('checklogin');
Route::get('/logout', [LoginController::class, 'logout'])->name('logout');
Route::post('/products/addproduct', [OrderController::class, 'addToCart'])->name('addtocart')->middleware('checklogin');
Route::post('/products/removeproduct', [OrderController::class, 'removeFromCart'])->name('removefromcart')->middleware('checklogin');
Route::get('/order', [PageController::class, 'order'])->name('order')->middleware('checklogin');
Route::post('/order/buy', [OrderController::class, 'payment'])->name('pay')->middleware('checklogin');
Route::get('/orderhistory', function(){return view('orderHistory');})->name('orderhistory')->middleware('checklogin');
Route::get('/products/details/{product}', [ProductDetailController::class, 'getProductPage'])->name('productdetail')->middleware('checklogin');
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::match(['get', 'post'], '/login/authenticate', [LoginController::class, 'authenticate'])->name('auth');
Route::get('/home', [PageController::class, 'home'])->name('home')->middleware('checklogin');
Route::match(['get', 'post'], '/products', [PageController::class, 'products'])->name('product')->middleware('checklogin');
Route::get('/logout', [LoginController::class, 'logout'])->name('logout');
Route::match(['get', 'post'], '/products/addproduct', [OrderController::class, 'addToCart'])->name('addtocart')->middleware('checklogin');
Route::match(['get', 'post'], '/products/removeproduct', [OrderController::class, 'removeFromCart'])->name('removefromcart')->middleware('checklogin');
Route::match(['get', 'post'], '/order', [PageController::class, 'order'])->name('order')->middleware('checklogin');
Route::match(['get', 'post'], '/order/buy', [OrderController::class, 'payment'])->name('pay')->middleware('checklogin');
Route::get('/orderhistory', function(){return view('orderHistory');})->name('orderhistory')->middleware('checklogin');
Route::get('/products/details/{product}', [ProductDetailController::class, 'getProductPage'])->name('productdetail')->middleware('checklogin');
是我用来将数据发送到路线的表格。
<div class="card-deck row">
@foreach (ProductController::getProduct() as $product)
@if($product->quantity > 0)
<div class="col-md-4 col-sm-12 my-3">
<div class="card h-100 mx-2" style="width: 18rem;margin-bottom: -1em">
<form action="{{ route('addtocart') }}" method="post">
@csrf
<a href="products/details/{{$product->id}}" class="" style="text-decoration: none; color: inherit;">
<div class="card-body d-flex flex-column">
<input type="hidden" name="productId" value="{{$product->id}}">
<h5 class="card-title" name="product">{{$product->name}}</h5>
<p class="card-text" name="description">{{$product->description}}</p>
</div>
</a>
<ul class="list-group list-group-flush">
<li class="list-group-item">
@if(count($product->categories) < 1)
No relevant categories found.
@else
@foreach ($product->categories as $category)
{{$category->name}}
@endforeach
@endif
</li>
</ul>
<div class="card-footer mt-auto">
<div class="input-group mb-3">
<input class="form-control" aria-describedby="basic-addon1" type="number" name="quantity" value="1" min="1" max="{{$product->quantity}}">
<div class="input-group-append">
<input class="btn btn-outline-secondary" type="submit" value="Buy">
</div>
</div>
</div>
</form>
</div>
</div>
@endif
@endforeach
</div>
这 有关。
这与我用来添加或从购物车中删除的功能
public function addToCart(Request $request){
$orderInfo = $this->getOrderInfo();
if($orderInfo === null){
$orderInfo = Order::create([
'customer_id' => Auth::user()->id,
'order_status' => 0
]);
}
if(Product::find($request->post('productId'))->quantity >= $request->post('quantity')){
if($orderInfo->products->contains($request->post('productId')) === true){
$orderInfo->products()->increment('order_quantity', $request->post('quantity'));
}else{
$orderInfo->products()->attach($request->post('productId'), ['order_quantity' => $request->post('quantity')]);
}
Product::where('id', $request->post('productId'))->decrement('quantity', $request->post('quantity'));
return redirect(route('product'));
}else{
return "Product not in Stock";
}
}
public function removeFromCart(Request $request){
$orderInfo = $this->getOrderInfo();
$orderQuantity = 0;
foreach($orderInfo->products as $order){
$orderQuantity = $order->pivot->order_quantity;
}
$orderInfo->products()->detach($request->post('delete'));
Product::where('id', $request->post('delete'))->increment('quantity', $orderQuantity);
return redirect(route('order'));
}
After redownloading my laravel project to my new pc from github, I got a new bug that didn't have before. The first entry of my shopping cart and of my products page can not go throught the assigned route given, but the rest of the entries work perfectly fine. I couldn't really find anything that resembled my problem in other questions.
I have tried to substitute the Route::post
with Route::match(['get', 'post']
, which solved the error, but had as a result that I couldn't add the first entry to the cart.
These are my routes before and after exchanging:
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::post('/login/authenticate', [LoginController::class, 'authenticate'])->name('auth');
Route::get('/home', [PageController::class, 'home'])->name('home')->middleware('checklogin');
Route::get('/products', [PageController::class, 'products'])->name('product')->middleware('checklogin');
Route::get('/logout', [LoginController::class, 'logout'])->name('logout');
Route::post('/products/addproduct', [OrderController::class, 'addToCart'])->name('addtocart')->middleware('checklogin');
Route::post('/products/removeproduct', [OrderController::class, 'removeFromCart'])->name('removefromcart')->middleware('checklogin');
Route::get('/order', [PageController::class, 'order'])->name('order')->middleware('checklogin');
Route::post('/order/buy', [OrderController::class, 'payment'])->name('pay')->middleware('checklogin');
Route::get('/orderhistory', function(){return view('orderHistory');})->name('orderhistory')->middleware('checklogin');
Route::get('/products/details/{product}', [ProductDetailController::class, 'getProductPage'])->name('productdetail')->middleware('checklogin');
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::match(['get', 'post'], '/login/authenticate', [LoginController::class, 'authenticate'])->name('auth');
Route::get('/home', [PageController::class, 'home'])->name('home')->middleware('checklogin');
Route::match(['get', 'post'], '/products', [PageController::class, 'products'])->name('product')->middleware('checklogin');
Route::get('/logout', [LoginController::class, 'logout'])->name('logout');
Route::match(['get', 'post'], '/products/addproduct', [OrderController::class, 'addToCart'])->name('addtocart')->middleware('checklogin');
Route::match(['get', 'post'], '/products/removeproduct', [OrderController::class, 'removeFromCart'])->name('removefromcart')->middleware('checklogin');
Route::match(['get', 'post'], '/order', [PageController::class, 'order'])->name('order')->middleware('checklogin');
Route::match(['get', 'post'], '/order/buy', [OrderController::class, 'payment'])->name('pay')->middleware('checklogin');
Route::get('/orderhistory', function(){return view('orderHistory');})->name('orderhistory')->middleware('checklogin');
Route::get('/products/details/{product}', [ProductDetailController::class, 'getProductPage'])->name('productdetail')->middleware('checklogin');
This is the form I use to send the data to the route.
<div class="card-deck row">
@foreach (ProductController::getProduct() as $product)
@if($product->quantity > 0)
<div class="col-md-4 col-sm-12 my-3">
<div class="card h-100 mx-2" style="width: 18rem;margin-bottom: -1em">
<form action="{{ route('addtocart') }}" method="post">
@csrf
<a href="products/details/{{$product->id}}" class="" style="text-decoration: none; color: inherit;">
<div class="card-body d-flex flex-column">
<input type="hidden" name="productId" value="{{$product->id}}">
<h5 class="card-title" name="product">{{$product->name}}</h5>
<p class="card-text" name="description">{{$product->description}}</p>
</div>
</a>
<ul class="list-group list-group-flush">
<li class="list-group-item">
@if(count($product->categories) < 1)
No relevant categories found.
@else
@foreach ($product->categories as $category)
{{$category->name}}
@endforeach
@endif
</li>
</ul>
<div class="card-footer mt-auto">
<div class="input-group mb-3">
<input class="form-control" aria-describedby="basic-addon1" type="number" name="quantity" value="1" min="1" max="{{$product->quantity}}">
<div class="input-group-append">
<input class="btn btn-outline-secondary" type="submit" value="Buy">
</div>
</div>
</div>
</form>
</div>
</div>
@endif
@endforeach
</div>
I have also no idea if this has anything to do with the function i use to add to or remove from the cart.
This is the function used to add the product to the cart and remove from the cart:
public function addToCart(Request $request){
$orderInfo = $this->getOrderInfo();
if($orderInfo === null){
$orderInfo = Order::create([
'customer_id' => Auth::user()->id,
'order_status' => 0
]);
}
if(Product::find($request->post('productId'))->quantity >= $request->post('quantity')){
if($orderInfo->products->contains($request->post('productId')) === true){
$orderInfo->products()->increment('order_quantity', $request->post('quantity'));
}else{
$orderInfo->products()->attach($request->post('productId'), ['order_quantity' => $request->post('quantity')]);
}
Product::where('id', $request->post('productId'))->decrement('quantity', $request->post('quantity'));
return redirect(route('product'));
}else{
return "Product not in Stock";
}
}
public function removeFromCart(Request $request){
$orderInfo = $this->getOrderInfo();
$orderQuantity = 0;
foreach($orderInfo->products as $order){
$orderQuantity = $order->pivot->order_quantity;
}
$orderInfo->products()->detach($request->post('delete'));
Product::where('id', $request->post('delete'))->increment('quantity', $orderQuantity);
return redirect(route('order'));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在留下了一段时间的问题之后,我回到了它,发现我不再出现错误代码,并且一切正常。我不知道问题是什么,也不知道它是如何固定的。
编辑
该错误是由项目Navbar中未封闭的
&gt;
标签引起的。关闭后,它解决了任何相关的错误消息。After leaving this problem for what it is for a while, I came back to it to find out that the error code I had no longer appears and that everything works fine and as it should. I don't know what the problem was, nor do I know how it fixed itself.
EDIT
The error was caused by an unclosed
<form>
tag in the navbar of the project. After closing it off it resolved any route related error messages.