Search code examples
laraveleloquenteager-loading

laravel elequent - eager loading - load using condition from previous table


I am trying to use the relationship as its shown in the code:

$lesson = Lesson::with(['teacher.teacherLessons' => function($teacherLesson){
    $teacherLesson->whereHas('category.courses', function($course){ 
        $course->where('id', DB::raw('`lessons`.`course_id`'));
    });
}]);

i want to use the same id that was loaded by the main query raw, instead to use DB::raw, how to get it?


Solution

  • The whereColumn/orWhereColumn method are there for just such a scenario.

    The whereColumn method may be used to verify that two columns are equal:

    $course->whereColumn('id', 'lessons.course_id');