Route Model binding in Laravel

What is Route-Model Binding?

As the name tells, to bind an Eloquent Model instance to a route (wildcard).

At this point in our Blog Project, we capture the post ID in the route definition for a single post, pass it to the callback function …


This content originally appeared on DEV Community and was authored by Arif Iqbal

What is Route-Model Binding?

As the name tells, to bind an Eloquent Model instance to a route (wildcard).

At this point in our Blog Project, we capture the post ID in the route definition for a single post, pass it to the callback function as paramere, and then send it to the findOrFail() method of our Post model.

Route::get('/posts/{post}', function ($id) {
    return view('post', [
        'post' => Post::findOrFail($id)
    ]);
});

Would it not be nicer to send the Post model instance directly to the callback function?

Route::get('/posts/{post}', function (Post $post) {
    return view('post', [
        'post' => $post
    ]);
});

It means we bound the Post model to the route /posts/{post}.

Things to remember about the Route-Model Binding:

  1. The Model type-hinted variable name in the route callback function should be the same as the route wildcard name. It means if your wildcard is {post} then callback variable name must be $post, otherwise it would not work.

  2. The default key that represents a Model is ID. It means, Laravel by default will assume the wildcard value to be the ID attribute of the Model instance. You can change it to any other unique key in the following two ways:

a) Update your route definition to mention the key with the wildcard as {post:slug}.

Route::get('/posts/{post:slug}', function (Post $post) {
    return view('post', [
        'post' => $post
    ]);
});

This is a relatively newer approach introduced in some recent version of Laravel.

b) Add a public function to your Model getRouteKeyName() and return the field name from it.

public function getRouteKeyName() {
    return 'slug';
}

This was the old way of changing the default key which still works.

Update you view files accordingly based on what key represents your Model. For example, if you changed it from id to slug, update your posts.blade.php as in the image

Route-Model Binding


This content originally appeared on DEV Community and was authored by Arif Iqbal


Print Share Comment Cite Upload Translate Updates
APA

Arif Iqbal | Sciencx (2021-12-14T16:47:57+00:00) Route Model binding in Laravel. Retrieved from https://www.scien.cx/2021/12/14/route-model-binding-in-laravel/

MLA
" » Route Model binding in Laravel." Arif Iqbal | Sciencx - Tuesday December 14, 2021, https://www.scien.cx/2021/12/14/route-model-binding-in-laravel/
HARVARD
Arif Iqbal | Sciencx Tuesday December 14, 2021 » Route Model binding in Laravel., viewed ,<https://www.scien.cx/2021/12/14/route-model-binding-in-laravel/>
VANCOUVER
Arif Iqbal | Sciencx - » Route Model binding in Laravel. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/14/route-model-binding-in-laravel/
CHICAGO
" » Route Model binding in Laravel." Arif Iqbal | Sciencx - Accessed . https://www.scien.cx/2021/12/14/route-model-binding-in-laravel/
IEEE
" » Route Model binding in Laravel." Arif Iqbal | Sciencx [Online]. Available: https://www.scien.cx/2021/12/14/route-model-binding-in-laravel/. [Accessed: ]
rf:citation
» Route Model binding in Laravel | Arif Iqbal | Sciencx | https://www.scien.cx/2021/12/14/route-model-binding-in-laravel/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.