如何在Laravel中嵌套资源

如何在Laravel中嵌套资源,laravel,resources,Laravel,Resources,我的routes.php文件中有这两条路由 $router->resource('user','UserController'); $router->resource('usertype','UserTypeController'); 但我真正想要的是获取用户并将其加入到usertype中 例子 用户表: id name usertype\u id 1行政2 约翰1 用户类型表: 身份证名称 1正常 2管理员 我想得到: 1“约翰”“正常” 2“管理员”“管理员” 我试过这个: p

我的routes.php文件中有这两条路由

$router->resource('user','UserController');
$router->resource('usertype','UserTypeController');
但我真正想要的是获取用户并将其加入到usertype中 例子 用户表: id name usertype\u id 1行政2 约翰1

用户类型表: 身份证名称 1正常 2管理员

我想得到: 1“约翰”“正常” 2“管理员”“管理员”

我试过这个:

public function __construct(User $user)
    {

        $this->user = $user->with('usertype');

    }
这不好用。它使用user.id设置关系 因此,它将进行的查询是

select * from user_types where user_types.id = users.id
应该是

select * from user_types where user_types.id = users.user_type_id
第二个问题是它对每个用户进行查询,
此处解释:

我找到了解决方案: 在用户控制器中:

public function __construct(User $user)
    {
        $this->user = $user->with('usertype')->with('company');
    }
在用户模型中:

public function usertype(){

    return $this->belongsTo('App\UserType','user_type_id','id');
}

public function company(){

    return $this->belongsTo('App\Company','company_id','id');
}

标题和问题不匹配。。。。第二个问题的邮政编码,而不是视频。