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
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.
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.
thanks
ReplyDelete