Laravel,eloquent 棘手的关系

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]

我需要"students of a particular subject" 无需根据 user/student 进行查询,因为它对 db 的点击率很高。

p.s。关系很好

我想这个查询对你有帮助。我不知道模型的功能,但我想你可以理解它。

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