I have some relationships e.g Department, Level, Course
Department belongsToMany Level belongsToMany Course
now I'm trying to fetch departments offering a course using this relationship
I tried using \Staudenmeir\EloquentHasManyDeep
but it keeps returning
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'levels.course_id' in 'field list' (SQL: select `departments`.*, `levels`.`course_id` as `laravel_through_key` from `departments` inner join `levels` on `levels`.`id` = `departments`.`level_id` where `levels`.`course_id` = 1 and `departments`.`deleted_at` is null and `levels`.`deleted_at` is null)
Course Model
public function levels()
{
return $this->belongsToMany(Level::class, LevelCourse::class);
}
public function departments()
{
return $this->hasManyThrough(Department::class, Level::class);
}
Level Model
public function courses()
{
return $this->belongsToMany(Course::class, LevelCourse::class)->withPivot('code');
}
public function departments()
{
return $this->belongsToMany(Department::class, DepartmentLevel::class);
}
This is the code I used in testing
dd(Course::first()->departments);