[英]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.