Bind a public URL to a slug

getRouteKeyName can return slug. The column has to be unique. The title is not a key.

For your AI agent

Copies a prompt. Paste it into the AI agent you already have open.

Read https://laravel-vuejs.space/laravel-slug-binding-vue.md
getRouteKeyName can return slug. The column has to be unique. The title is not a key.
Do not use the title as getRouteKeyName().
Do not look the slug up in Vue from a list of every post.
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

People searching “Laravel slug route Vue” put the title in the URL and then a renamed post 404s every old link. Store a slug and bind that column.

TL;DR

  • getRouteKeyName can return slug.
  • The column has to be unique.
  • The title is not a key.
  • Do not use the title as getRouteKeyName().

Who it's for: Laravel + Vue developers

Applies to: Laravel 13 · Vue 3

The fix#

php
public function getRouteKeyName(): string
{
    return 'slug';
}
php
Route::get('/posts/{post}', [PostController::class, 'show'])->name('posts.show');

/posts/hello-world resolves where slug = hello-world. The Vue page still receives the post prop. Generate the slug once when the post is created, from the title, and keep it stable when the title changes. A unique index on slug is what makes the binding mean one row.

Validate the slug with a rule that allows lowercase letters, numbers, and hyphens, plus Rule::unique('posts', 'slug')->ignore($post->id) on update.

What to avoid#

Gotcha

Do not use the title as getRouteKeyName(). Spaces and renamed titles break the Wayfinder URL you already sent to the browser. Do not look the slug up in Vue from a list of every post.

FAQ#

How do I bind a public URL to a slug?

getRouteKeyName can return slug. The column has to be unique. The title is not a key.

What goes wrong with Laravel slug route Vue?

Many developers put the title in the URL and then a renamed post 404s every old link. Do not use the title as getRouteKeyName().

Found this useful?

Ask questions, share what you built.

Join thousands of Laravel and Vue developers in our Facebook group, or get new posts in your Telegram feed. Sharing this article helps too.

Keep reading

Newsletter

Get the best of Laravel & Vue, once a month.

One short email with new guides, release notes worth reading and hand-picked community links. No spam, unsubscribe any time.