Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wanjaswilly's avatar

Adding a useless post title to a post url

I have a blog and it fetches data from a model posts. I use a Route::resources([ 'post'=> PostController::class); for urls. This generates urls like mysite.com/post/{id}. i would love to add a url-safe title to the url for it to read mysite.com/post/{id}/this-is-an-example-title-for-this-post. How would i go about it?

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

I did this quite a few years ago like this

href="/talk/{{ $talk->id }}/{{ $talk->slug }}"

$talk->slug is an accessor that creates a slug on the fly based on the talk title

Only the talk->id is used in the resolution of the correct talk, so if the user changes the title of their talk, then the link is different but no previously shared links are broken.

So the route looks like;

Route::get('talk/{talk}/{slug?}', [TalksController::class, 'show'])->name('talk.show');

You can see the slug is optional with the talk attribute used for route model binding.

As seen at https://speakernet.co.uk

Feel free to try it, click a link to a talk, take off the slug and it still works, or even change to a completely random name and it will still work. The point is, the talk title can be part of the link for SEO purposes.

Please or to participate in this conversation.