route model binding laraver code example
Example 1: explicit route model binding in laravel
public function boot()
{
Route::model('unique_key', Blog::class);
Route::bind('unique_key', function ($value) {
return Blog::findOrFail($value);
});
}
Example 2: laravel model bind with route in model
public function resolveRouteBinding($value, $field = null)
{
return $this->where('name', $value)->firstOrFail();
}
Example 3: 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();
}