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'));
}
