Friday, October 16, 2015

Route Model Binding using laravel 5.1

Route Model binding using Laravel 5.1:

What is Route Model Binding in laravel?
Ans: Laravel model binding provides a convenient way to inject class instances into  routes. For example, instead of injecting a user's ID, you can inject the entire User class instance that matches the given ID.

1. Open route.php and write following code .

Route::model('users', 'User');
Route::bind('users', function($value, $route) {
    return App\Task::whereSlug($value)->first();
)};
Route::resource('users', 'UsersController');

 Note: where User is Model name 

2. use users/{slug} as url instead of users/{id}

3. use controller method update({slug}) instead of update({id})

Thanks

No comments:

Post a Comment