繁体   English   中英

Laravel,雄辩的棘手关系

[英]Laravel, eloquent trickey relations

这是对数据库的描述==>

Users [ hasOne Student]
Students (with user_id) [not all users are students, so it's "belongsTo" User] 

Groups [hasOne subject]
Subjects (with group_id) [not all groups are subjects, i use groups for other things too, so it's "belongsTo" Group]

group_user  [simple pivot table, connecting users and groups]

我需要获得“特定学科的学生”,而无需按用户/学生进行查询,因为它对数据库的影响很大。

ps关系设置良好。

我认为此查询将为您提供帮助。我不知道该模型的功能,但我认为您可以理解。

$students = Subject::with(['group.users.student'])
                    ->where('subject_name','=',$subjectName)
                    ->get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM