加入Laravel的多个SQL表
我困扰着尝试将多个桌子加在一起。我有一个用户表,用户的名字和其他三张桌子适合患者,医生和约会。约会表有患者和医生的ID。例如,我只能加入两张桌子,例如约会和患者,以显示患者的数据如下。
$data = User::join('appointments','users.id','=','appointments.user_id')
我想同时知道如何为患者和医生做这件事。 DB中的用户表具有一个名称,姓氏,电话号码和角色ID(医生1,患者为3)。约会表有user_id,doctor_id,日期和时间。我想获取用户名和医生的名字。
I'm stuck trying to join multiple tables together. I have a user table with the users' names and three other tables for patients, doctors, and appointments. The appointment table has the patient's and doctor's id. I can only join two tables, for example, the appointments and the patient, to show the patients' data like the following.
$data = User::join('appointments','users.id','=','appointments.user_id')
I want to know how to do this for the patient and doctor simultaneously. The users' table in DB has a name, last name, phone number, and role id(1 for doctor, 3 for patient). The appointment table has user_id, doctor_id, date, and time. I want to get the user name and the doctor's name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试此代码
示例:
laravel Query Query Builder
Try this code
Example:
Laravel Query Builder
尝试一下:
laravel查询构建器
Try this:
Laravel Query Builder
您可以做任何提到的方法。您也可以在模型之间建立关系。然后,当您调用用户模型时,只需
通过雄伟的魔法使用所有订单,并且该用户的所有约会都将添加到收藏的关系中。在Laravel文档中查找一到许多关系。
You could do either of the ways mentioned. You could also do a relationship between the models. Then when you call the User model just use
Through eloquent magic all the orders and all the appointments for that user will be added to the relationship on your collection. Look up one to many relationships in the Laravel docs.