// In RouteServiceProvider
public function boot()
{
//don't forget import model at the top
Route::model('unique_key', Blog::class);
Route::bind('unique_key', function ($value) {
return Blog::findOrFail($value);
//return Blog::where('something', $value)->firstOrFail();
});
//default laravel codes
}
Route::get('posts/{post:slug}', function(Post $post) {
return view('post', [
'post' => $post
]);
});
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @param string|null $field
* @return IlluminateDatabaseEloquentModel|null
*/
public function resolveRouteBinding($value, $field = null)
{
return $this->where('name', $value)->firstOrFail();
}