Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.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
Php 具有两个表的laravel联接查询每个表都有两个where条件_Php_Laravel - Fatal编程技术网

Php 具有两个表的laravel联接查询每个表都有两个where条件

Php 具有两个表的laravel联接查询每个表都有两个where条件,php,laravel,Php,Laravel,这是我的控制器,我需要添加一个,其中表章节上的条件为启用=0 $module = $course->modules() ->with('chapters') ->join('course_student', 'course_student.module_id', '=', 'modules.id') ->select('*', DB::raw('cou

这是我的控制器,我需要添加一个,其中表章节上的条件为启用=0

 $module = $course->modules()
                    ->with('chapters')
                    ->join('course_student', 'course_student.module_id', '=', 'modules.id')
                    ->select('*', DB::raw('course_student.module_id as id'))
                    ->where('is_enabled', 1)
                    ->where('course_student.student_id', $user->id)
                    ->where('course_student.test_completed', 0)
                    ->where('course_student.ends_on', '>', Carbon::now())
                    ->orderBy('order')
                    ->first();

如果希望有条件地选择关系,则应使用whereHas方法

这些方法允许您将自定义约束添加到 关系约束,例如检查注释的内容

留给您的代码如下:

$module = $course->modules()
    ->whereHas('chapters', function ($query) {
        $query->where('is_enabled', 0);
    })
    ->join('course_student', 'course_student.module_id', '=', 'modules.id')
    ->select('*', DB::raw('course_student.module_id as id'))
    ->where('is_enabled', 1)
    ->where('course_student.student_id', $user->id)
    ->where('course_student.test_completed', 0)
    ->where('course_student.ends_on', '>', Carbon::now())
    ->orderBy('order')
    ->first();

S.O.不是代码完成服务。请说明到目前为止您尝试了什么以及您收到的错误。如何在章节表中包含where条件您只需执行->where('chapters.chapter_name',$chapterName)