skip to Main Content

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 AppHttpControllers;
    
    use IlluminateHttpRequest;
    use IlluminateSupportFacadesGate;
    use AppHttpRequestsProduct{ImageRequest,OfferRequest,DeliveryRequest,InformationsRequest};
    use AppModels{Product,Image,Offer,Delivery,Category,Favorite};
    use IlluminateSupportStr;
    
    class ProductController extends Controller
    {

    /**
     * Change product stock setting
     *
     * @param $switch
     * @return IlluminateHttpRedirectResponse
     *
     */
    public function changeStock($switch): IlluminateHttpRedirectResponse
    {
        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>

2

Answers


  1. you need to pass both product and turn parameters to the route
    you likely have access to the $product object in your blade file

    update your code like this :

    <div class="btn-group btn-group-sm w-100 my-1 mb-4" role="group" aria-label="Basic example">
                        <a href="{{ route('change.stock', [$product->id, 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', [$product->id, 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>
    
    Login or Signup to reply.
  2. Router:

    Route::get('/product/stock/{product}/{turn}', [ProductController::class, 'changeStock'])->name('change.stock');
    

    Controller:

    public function changeStock(Product $product, $turn): IlluminateHttpRedirectResponse
    {
        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])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search