Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Laravel

Route Model Binding in Laravel

Automatically inject Eloquent models into routes and controllers using implicit and custom bindings.

Route Model Binding in Laravel — a practical guide to Laravel route model binding with clear examples you can reuse in real projects.

Laravel Tutorial Series (27/27). Prefer one page? Read the complete Laravel tutorial (all 27 topics).

Short description

Route model binding resolves `{post}` into a `Post` model instance automatically — less boilerplate `findOrFail`.

Implicit binding

Route::get('/posts/{post}', [PostController::class, 'show']);

public function show(Post $post)
{
    return view('posts.show', compact('post'));
}

Visiting `/posts/5` injects the Post with id 5 (404 if missing).

Bind by slug column

// In model
public function getRouteKeyName(): string
{
    return 'slug';
}

URL becomes `/posts/hello-laravel`.

Scoped bindings (nested)

Route::get('/users/{user}/posts/{post:slug}', function (User $user, Post $post) {
    return $post;
})->scopeBindings();

Custom binding in boot

Route::bind('post', function (string $value) {
    return Post::where('slug', $value)->where('is_published', true)->firstOrFail();
});

Leave a reply

Your email address will not be published. Required fields are marked *