How can I used slug instead of id in laravel 8

  laravel, laravel-8, slug

How can I use slug instead of id in laravel 8

This is my public function in controller

public function singleProduct($slug){
        $product = Product::findOrFail($slug);
        return view('frontend.product.product', compact('product'));

and this is the route

Route::get('/product/{slug}', [HomeController::class, 'singleProduct']);

and this is from where it will go to the single product

<a href="{{ url('/product/'.$product->slug) }}" class="customBtn4">Read More</a>

Source: Laravel

Leave a Reply