When injecting a model ID to a route or controller action, you will often query to retrieve the model that corresponds to that ID. Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes. For example, instead of injecting a user’s ID, you can inject the entire User model instance that matches the given ID.
Route::get('api/users/{user}', function (App\User $user) { return $user->email; });
あーなんか集中力切れてきた。。
Customizing The Key Name
If you would like model binding to use a database column other than id when retrieving a given model class, you may override the getRouteKeyName method on the Eloquent model:
public function getRouteKeyName() { return 'slug'; }
Explicit Binding
To register an explicit binding, use the router’s model method to specify the class for a given parameter. You should define your explicit model bindings in the boot method of the RouteServiceProvider class:
あ、なんか聞いたことあるぞ。
public function boot() { parent::boot(); Route::model('user', App\User::class); }