Там в скоупах постійно дублюється код:
->whereIn('lesson_id', function ($query) {
$query->select('id')->from('lessons')->whereIn('section_id', function ($query) {
$query->select('id')->from('sections')->whereRaw('sections
.course_id
= courses
.id
');
});
});