Thursday 30 March 2017

New feature in laravel 5.4 Route and Model Binding

In this article i will explain route model binding .

Route model binding provides a useful way to automatically method-inject a model's instance into your route closures, or controller actions. For example, if a user visits /users/1, thanks to route model binding, we can make Laravel automatically fetch the Users with an id of 1, and then inject that instance into your controller action.                            

routes/web.php

Route::get('users/{users}', 'UsersController@show');

app/Http/Controllers/UsersController.php

public function show($id)
    {
       $users = User::find($id);
       return $users;

    }



Instead of above code we can bind the model to function. Please find below code.


app/Http/Controllers/UsersController.php

public function show(User $users)
    {
       return $users;

    }

User is a model name and $users form route parameter.

1 comment:

cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in Laravel

  WampServer: Download this file:  http://curl.haxx.se/ca/cacert.pem Place this file in the  C:\wamp64\bin\php\php7.1.9  folder Open  php.in...

Popular Articles