如何 select 具有多重关系的实例?

How to select instances with multiple relations?

我有一些型号 Featured_Course_RequestCourse_RequestResponseTeacherFeatured_Course_Request hasOne Course_RequestCourse_Request hasMany Response 通过 Teacher

我想得到唯一的 Featured_Course_Requests which logged teacher has not responded (have no Response by logged teacher.) 我该怎么做?

我试图用下面的代码实现它,但它没有给出正确的输出。

 $featured_course_request = Featured_Course_Resquest::whereRaw('remaining_coins >=  coins_per_click')->where('status', '=', 'open')
    ->whereHas('courseRequest', function($q) use ($teacher){                                                            
    $q->whereHas('responses', function($qe) use ($teacher){
        $qe->where('teacherID', '!=', $teacher->id);
            });

    });

您可以使用点语法定位嵌套关系:'courseRequest.responses' 此外,您还需要 whereDoesntHave 而不是 whereHas:

$featured_course_request = Featured_Course_Resquest::whereRaw('remaining_coins >=  coins_per_click')
    ->where('status', '=', 'open')
    ->whereDoesntHave('courseRequest.responses', function($q) use ($teacher){
        $qe->where('teacherID', '=', $teacher->id);
    })
    ->get();