route model binding laravel 7 code example
Example 1: laravel route
Route::get('user/{name}', function (Request $request) {
return $request->route('name');
});
Example 2: laravel route
Route::view('/welcome', 'welcome');
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
Example 3: laravel model bind with route in model
public function resolveRouteBinding($value, $field = null)
{
return $this->where('name', $value)->firstOrFail();
}
Example 4: laravel route model binding
Route::bind('user', function($value)
{
return User::where('name', $value)->first();
});
public function resolveRouteBinding($value, $field = null)
{
return $this->where('name', $value)->firstOrFail();
}