我正在使用luman和Database Query Builder从数据库中获取完整的用户信息。laravel查询生成器:加入依赖于主要内容
首先,请在我的数据库结构以锁: 我有一个表称为users
和一系列其他表的使用有关的用户群体(例如:secretaries
,patients
,doctors
和admins
)用于存储有关的其他信息用户。
此外,要确定用户访问,我有user
表中的level
列,它可以有一个值为enum:'admin', 'doctor', 'secretary', 'patient'
。
所以,我想通过join
和select
得到使用一个查询这些信息。
我的训练码是这样的:
$userInfo = User::where("userID", $userID)
->limit(1)
->join('[GROUP_TABLE_NAME]', function ($join) {
$join->on('user.userID', '=', '[GROUP_TABLE_NAME]' .'.'.
'[GROUP_NAME]' . 'ID');
})
->get();
的GROUP_NAME
来自level
列上user
表,并可以基于该GROUP_NAME
值建成GROUP_TABLE_NAME
(例如:['secretary' => 'secretaries' , 'patient' => 'patients' , ...]
) 。
任何想法来处理使用laravel查询生成器的连接结构?
查看多态关系https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations –