我在routes.php文件中有这两条路由
$router->resource('user','UserController');
$router->resource('usertype','UserTypeController');
但我真正想要的是获取用户并加入usertype与它的示例用户表:id name usertype_id 1 admin 2 2 john 1
usertype表:id name 1 normal 2 admin
我想得到: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
第二个问题是它为每个用户执行查询,在此解释:https://www.youtube.com/watch?v=swhWRMkpVsg
1 回答
我找到了sollution:在用户控制器中:
在用户模型中: