Search code examples
phplaravellaravel-bladelaravel-5.3

Multiple parameter to route error laravel


I have method in my controller (singleProduct):

    public function singleProduct($slug)
{
   $product= Product::where('slug','=', $slug)->first();
   return view('public.product.show')->withProduct($product);
}

And my route is:

Route::get('/{category}/{slug}',['as' => 'single.product', 'uses' => 'LinkController@singleProduct']);

My code in view:

<a href="{{ route('single.product', $product->category, $product->slug) }}">{{$product->title}}</a>

Though i have passed both required parameter for route.My route is returning an error of:

Missing required parameters for [Route: single.product] [URI: {category}/{slug}].

Solution

  • The correct way for defining route params is like:

    route('single.product', ['category' => $product->category, 'slug' => $product->slug])
    

    So your route in view will be as:

    <a href="{{ route('single.product', ['category' => $product->category, 'slug' => $product->slug]) }}">{{$product->title}}</a>
    

    Docs