Route :: model在Laravel中意味着什么?

时间:2016-10-25 10:49:17

标签: php laravel laravel-5 laravel-5.3 laravel-routing

任何人都可以解释这些线吗?它是如何工作的?

public function boot()
{
     parent::boot();

     Route::model('user', App\User::class);
}

接下来,定义包含{user}参数的路由:

$router->get('profile/{user}', function(App\User $user) {
  //
});

1 个答案:

答案 0 :(得分:4)

这称为Route Model Explicit Binding

有了这个:

db.Model(&mymodels).Table('temp_table1').Column("mymodel.id", "mymodel.name").Select()

您说:当路由中使用Route::model('user', App\User::class); 字符串作为参数时,为我创建一个'user'模型,其id与传递给该参数的参数相同路线。然后在路由方法处理程序上注入模型。

例如,网址:App\User::class将匹配此路线:

'profile/10'

Laravel

会自动获取id为10的$router->get('profile/{user}', function(App\User $user) { // }); 模型

从一般观点来看,通常在您的路线中,您可以执行以下操作:

App\User

使用路径模型绑定,您可以执行以下操作:

public function edit($id)
{
    //fetch the user from db...
    $user = User::find($id);

    //do something with the $user...
}

避免从数据库中获取模型:Laravel将为您完成