Bind a Vue page to a route model

Type-hint Post $post and the URL {post} resolves the row. A missing row is a 404.

For your AI agent

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

Read https://laravel-vuejs.space/laravel-route-model-binding-vue.md
Type-hint Post $post and the URL {post} resolves the row. A missing row is a 404.
Do not take the id from the query string when it is already in the path.
Do not pass the whole model if the page needs two columns.
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

People searching “Laravel route model binding Vue” still Post::find($id) and then forget the 404. Type-hint the model. The parameter name matches the route segment. The CRUD guide uses this on edit and update.

TL;DR

  • Type-hint Post $post and the URL {post} resolves the row.
  • A missing row is a 404.
  • Do not take the id from the query string when it is already in the path.
  • Do not pass the whole model if the page needs two columns.

Who it's for: Laravel + Vue developers on Inertia

Applies to: Laravel 13 · Inertia 3 · Vue 3

The fix#

php
Route::get('/posts/{post}', [PostController::class, 'show'])->name('posts.show');
php
public function show(Post $post): Response
{
    return Inertia::render('posts/Show', [
        'post' => $post->only(['id', 'title']),
    ]);
}

/posts/15 loads post 15. /posts/99999 is a 404 before the method runs. The Vue page always receives a post.

A nested route such as /posts/{post}/comments/{comment} should scope the comment to the post. scopeBindings() on the route group does that. Without it, a comment id from another post still resolves.

What to avoid#

Gotcha

Do not take the id from the query string when it is already in the path. Do not pass the whole model if the page needs two columns. only() keeps the payload small.

FAQ#

How do I bind a Vue page to a route model?

Type-hint Post $post and the URL {post} resolves the row. A missing row is a 404.

What goes wrong with Laravel route model binding Vue?

Many developers still Post::find($id) and then forget the 404. Do not take the id from the query string when it is already in the path.

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.