Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 不存在的Laravel不能正常工作_Database_Postgresql_Laravel_Eloquent - Fatal编程技术网

Database 不存在的Laravel不能正常工作

Database 不存在的Laravel不能正常工作,database,postgresql,laravel,eloquent,Database,Postgresql,Laravel,Eloquent,据我所知,whereNotExists应该排除传递的闭包中的所有查询。 然而,我得到了意想不到的结果 我想做的是返回所有申请关闭条件的学生;未被家长设定为缺席或缺席的学生。我得到的是一个空的students数组[] 我做错了什么 $students = DB::table('rounds') ->where('rounds.bus_id', '=', $bus_id) ->join('rounds_student

据我所知,whereNotExists应该排除传递的闭包中的所有查询。 然而,我得到了意想不到的结果

我想做的是返回所有申请关闭条件的学生;未被家长设定为缺席或缺席的学生。我得到的是一个空的students数组[]

我做错了什么

    $students = DB::table('rounds')
                ->where('rounds.bus_id', '=', $bus_id)
                ->join('rounds_students', 'rounds_students.round_id', 'rounds.id')
                ->whereNotExists(function ($query) {
                     $query->select(DB::raw(1))
                        ->from('student_history')
                        ->where('student_history.student_id', '=', 'rounds_students.student_id')
                        ->where('student_history.activity_type', '=', 'absent')
                        ->orWhere('student_history.activity_type', '=', 'absent-by-parent');
                    })
                ->join('students', 'students.id', 'rounds_students.student_id')
                ->select('students.name')
                ->get();
            return $students;
  • 必须将原始表达式与
    whereRaw()
    where(DB::raw(“…”)
    一起使用,才能在
    NOT EXISTS
    子句中定义相关条件。否则,
    'rounds\u students.student\u id'
    将作为字符串值传递,这不是您想要的
  • 小心
    s和
    s在您的
    WHERE
    条件下!你的不正确。将其更改为
    ,其中
    ,使其更加简洁
  • 另外,您实际上不需要在
    EXISTS
    子句中使用
    select(DB::RAW(1))
    。数据库优化器知道他们不需要将任何结果集返回到外部查询。这可能有助于减少代码膨胀

    试一试

    $students=DB::table('rounds')
    ->选择('students.name')
    ->加入('rounds\u students'、'rounds\u students.round\u id'、'rounds.id')
    ->加入('students','students.id','rounds\u students.student\u id'))
    ->其中('rounds.bus\u id',$bus\u id)
    ->whereNotExists(函数($query){
    $query->from('student\u history')
    ->whereRaw('student\u history.student\u id=rounds\u students.student\u id')
    ->其中('student_history.activity_type',['缺席','家长缺席');
    })
    ->get();
    
  • 必须将原始表达式与
    whereRaw()
    where(DB::raw(“…”)
    一起使用,才能在
    NOT EXISTS
    子句中定义相关条件。否则,
    'rounds\u students.student\u id'
    将作为字符串值传递,这不是您想要的
  • 小心
    s和
    s在您的
    WHERE
    条件下!你的不正确。将其更改为
    ,其中
    ,使其更加简洁
  • 另外,您实际上不需要在
    EXISTS
    子句中使用
    select(DB::RAW(1))
    。数据库优化器知道他们不需要将任何结果集返回到外部查询。这可能有助于减少代码膨胀

    试一试

    $students=DB::table('rounds')
    ->选择('students.name')
    ->加入('rounds\u students'、'rounds\u students.round\u id'、'rounds.id')
    ->加入('students','students.id','rounds\u students.student\u id'))
    ->其中('rounds.bus\u id',$bus\u id)
    ->whereNotExists(函数($query){
    $query->from('student\u history')
    ->whereRaw('student\u history.student\u id=rounds\u students.student\u id')
    ->其中('student_history.activity_type',['缺席','家长缺席');
    })
    ->get();
    
    通常
    不存在
    子查询使用外部查询的某些元素。“在这里,你根本就没有这么做。”apokryfos我已经这么做了,但忘了在问题中提到它。用它更新了问题。我仍然得到一个空的$students数组。通常,
    不存在
    子查询使用外部查询的某些元素。“在这里,你根本就没有这么做。”apokryfos我已经这么做了,但忘了在问题中提到它。用它更新了问题。我仍然有一个空的$students数组。