1// In RouteServiceProvider
2 public function boot()
3 {
4 //don't forget import model at the top
5 Route::model('unique_key', Blog::class);
6 Route::bind('unique_key', function ($value) {
7 return Blog::findOrFail($value);
8 //return Blog::where('something', $value)->firstOrFail();
9 });
10
11 //default laravel codes
12 }
1public function boot()
2{
3 parent::boot();
4
5 Route::model('user', App\User::class);
6}
1Route::bind('user', function($value)
2{
3 return User::where('name', $value)->first();
4});
5
6// We can And Do also this in model..
7
8/**
9 * Retrieve the model for a bound value.
10 *
11 * @param mixed $value
12 * @param string|null $field
13 * @return \Illuminate\Database\Eloquent\Model|null
14 */
15public function resolveRouteBinding($value, $field = null)
16{
17 return $this->where('name', $value)->firstOrFail();
18}