Laravel hasManyThrough relationship

November 14, 2020

You can find hasManyThrough relationship in Laravel Eloquent

teachers
    id - integer
    name - string

classes
    id - integer
    teacher_id - integer
    name - string

students
    id - integer
    class_id - integer
    name - string

If you want to get the list of students whom the given teacher teaches for you can use the hasManyThrough

class Teacher extends Model
{
    public function students()
    {
        return $this->hasManyThrough('App\Models\Class', 'App\Models\Student');
    }
}