【发布时间】:2019-02-11 13:39:05
【问题描述】:
我有一个应用程序,我想根据子条件获取父记录。当前的问题是我有学生,他们有多个研究领域,研究领域属于一个学院。数据透视表students_study_fields 具有属性study_status_id。
我需要的是,例如,获取属于“prf”教员的所有学生及其学习领域,并且 pivot 拥有study_status_id = 1。
所以我写了一个这样的查询。
return Student::with(['studyfields' => function ($query1) use ($studyStatusId, $facultyAbbreviation) {
$query1->whereHas('pivot', function ($query2) use ($studyStatusId, $facultyAbbreviation) {
$query2->where('study_status_id', $studyStatusId);
});
$query1->whereHas('studyprogram', function ($query4) use ($facultyAbbreviation) {
$query4->whereHas('faculty', function ($query5) use ($facultyAbbreviation) {
$query5->where('abbreviation', $facultyAbbreviation);
});
});
}])->get();
但是这个查询也获取了study_status_id = 2 的学生,因为存在同一研究领域(其代码)与学生相关的记录,其中study_status_id = 1。
因此,如果某处存在带有status = 1 的记录,但仅当当前行具有status = 1 时,我不想包含此研究字段
【问题讨论】: