So having this strange problem in laravel. I am trying to update a boolean value in my database but i keep getting this strange error in my log:
Missing required parameter for [Route: change.stock] [URI: product/stock/{product}/{turn}] [Missing parameter: turn]
Anybody maybe knows what i am over seeing here.
My route:
Route::get('/product/stock/{product}/{turn}', [ProductController::class, 'changeStock'])->name('change.stock');
My Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Requests\Product\{ImageRequest,OfferRequest,DeliveryRequest,InformationsRequest};
use App\Models\{Product,Image,Offer,Delivery,Category,Favorite};
use Illuminate\Support\Str;
class ProductController extends Controller
{
/**
* Change product stock setting
*
* @param $switch
* @return \Illuminate\Http\RedirectResponse
*
*/
public function changeStock($switch): \Illuminate\Http\RedirectResponse
{
try{
$this -> setStock($switch);
session() -> flash('success', 'You have changed your product stock setting');
}
catch (RequestException $e){
session() -> flash('errormessage', $e -> getMessage());
}
return redirect() -> back();
}
/**
* Sets the product stock status
*
* @param $switch
* @throws RequestException
*/
public function setStock($switch)
{
// sets stock status of product
$this -> in_stock = $switch == 1;
$this -> product() -> in_stock -> save();
}
The blade file:
<div class="btn-group btn-group-sm w-100 my-1 mb-4" role="group" aria-label="Basic example">
<a href="{{ route('change.stock', 1) }}" class="btn @if
($product -> in_stock == 1)
btn-success btn-sm @else btn-outline-success btn-sm @endif">In Stock</a>
<a href="{{ route('change.stock', 0) }}" class="btn @if
($product -> in_stock == 0)
btn-danger btn-sm @else btn-outline-danger btn-sm @endif">Set Out of Stock</a>
</div>
Router:
Route::get('/product/stock/{product}/{turn}', [ProductController::class, 'changeStock'])->name('change.stock');
Controller:
public function changeStock(Product $product, $turn): \Illuminate\Http\RedirectResponse
{
try {
$product->update(['in_stock' => $turn]);
session()->flash('success', 'You have changed your product stock setting');
} catch (RequestException $e) {
session()->flash('errormessage', $e->getMessage());
}
return redirect()->back();
}
Using in blade:
{{ route('change.stock', [$product->id, 1])