Php 具有两个表的laravel联接查询每个表都有两个where条件
这是我的控制器,我需要添加一个,其中表章节上的条件为启用=0Php 具有两个表的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
$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)