【问题标题】:Laravel - whereHas with where affect other rowsLaravel - whereHas with where 影响其他行
【发布时间】: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 时,我不想包含此研究字段

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您需要链接查询...

    return Student::with(['studyfields' => function ($query1) use ($studyStatusId, $facultyAbbreviation) {
        $query1->whereHas('pivot', function ($query2) use ($studyStatusId, $facultyAbbreviation) {
             $query2->where('study_status_id', $studyStatusId);
        })->whereHas('studyprogram', function ($query4) use ($facultyAbbreviation) {
             $query4->whereHas('faculty', function ($query5) use ($facultyAbbreviation) {
                 $query5->where('abbreviation', $facultyAbbreviation);
             });
        });
    }])->get();
    

    否则它会重新开始查询1,所以你不会得到AND这种查询,只能得到第二部分

    旁注:但是,我想警告您,whereHas 是一个慢查询,如果您有很多行,因为它遍历每个值。我个人更喜欢通过简单的->where 查询来获取 ID,并使用->whereIn 方法。

    【讨论】:

    • 嗯,这很奇怪。您可以尝试对$studyStatusId 和$facultyAbbreviation 进行硬编码吗?或注释掉第二部分->whereHas('studyprogram'...` 注释掉以查看导致问题的部分。
    • 如果我尝试链 Student::with('studyfields')->whereHas(studyfields) .... ->whereHas(studyprogram) ... 它会按教员过滤,但 study_status 的行为仍然相同......我也尝试了 force study_status 和 Faculty_abb
    【解决方案2】:

    我找到了适合我情况的解决方案

    $students = Student::with(['studyfields' => function ($q) use ($studyStatusId) {
                $q->whereHas('pivot')->where('study_status_id', $studyStatusId);
            }])
                ->whereHas('studyfields', function ($q) use ($facultyAbbreviation) {
                    $q->whereHas('studyprogram', function ($q) use ($facultyAbbreviation) {
                        $q->where('faculty_abbreviation', $facultyAbbreviation);
                    });
                })
                ->get();
            $students = $students->filter(function ($student) {
                return count($student->studyfields) > 0;
            })->values();
    

    上面的查询从特定学院获取所有学生,如果 studyfields 数组不包含特定的 study_status,则保留空数组,以便稍后我可以从空数组中过滤集合,假设每个学生至少属于一个 studyfield。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2021-07-01
      • 2015-06-17
      • 1970-01-01
      • 2016-06-17
      • 2017-11-05
      相关资源
      最近更新 更多