Ad

Laravel Blade Route To Show Method On Title

- 1 answer

I've got this route in a blade template:

<a target="_blank" rel="nofollow noreferrer" href="{{route('docs', ['title' => $doc->slug])}}">{{$doc->title}}</a>

This gives the link an href like: /docs?title=installation

This works so that is nice. Laravel automatically translates this URL into /docs/installation.

But I rather have the href containing the /docs/installation path.

How to do that?

Update: This is how the Route looks like in web.php.

Route::resource('docs', 'DocsController')->name('index', 'docs');
Ad

Answer

I guess what you want to do is

<a target="_blank" rel="nofollow noreferrer" href="{{route('docs.show', $doc->slug)}}">{{$doc->title}}</a> 
{{-- This route will generate an URL looks like 'docs/{whatever}'  --}}

Then in your web.php file

Route::resource('docs', 'DocsController')->name('docs');

And Finally in your DocsController

public function show($slug) {
   //...
}
Ad
source: stackoverflow.com
Ad