Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rretzko's avatar
Level 15

hasManyThrough() problem: School-Student-Teacher

I've just started using Laravel after 10-years of custom php development experience. I'm re-writing an education platform under Laravel and am hitting a wall with the hasManyThrough() implementation. Here's the scenario:

Models

class User extends Authenticatable
{
    public function schools()
    {
        return $this->belongsToMany(School::class);
    }
}
class Student extends Model 
{    
    protected $primaryKey = 'user_id';
 
    public $incrementing = false;

    public function person()
    {
        return $this->hasOne(Person::class, 'user_id');
    }
    
    
    public function teachers()
    {
        return $this->hasManyThrough(
                'App\Teacher',  //target model 
                'App\School');   //through-model
    }


class Teacher extends Model
{
    protected $primaryKey = 'user_id';
    public $incrementing = false;
    
    public function Person()
    {
        return $this->hasOne(Person::class, 'user_id');
    }
    
    public function Schools()
    {
        return $this->belongsToMany('App\School', 'school_teacher', 'user_id', 'school_id');
    }
    
    public function students()
    {
        return $this->belongsToMany('App\Student', 'student_teacher', 'teacher_id', 'student_id');
    }
}

class School extends Model
{
    /**
     * School_User is a many-to-many relationship
     */
    public function teachers()
    {
        return $this->belongsToMany('App\Teacher', 'school_teacher', 'school_id', 'user_id');
    }
    
}

Tables:

User: id, name, password
Student: user_id, class_of
Teacher: user_id, curriculum_id
School: id, name
school_teacher: school_id, user_id (for many-to-many schools-to-teachers)
school_user: school_id, user_id (for many-to-many schools-to-students)

The student and the teacher each belongs-to school(s) but selectively relate/associate with each other in a many-to-many through the school model, i.e.: a teacher can have 0-many students, 1-many schools. a student can have 1-many teachers, 1-many schools. a school can have 0-many teachers, 0-many students Teachers and Students relate to each other through the School

My custom implementation used a school-student-teacher pivot table to make this association, but I'm not getting how this would be implemented in Laravel using the hasManyThrough() helper.

Any guidance and/or reference links would be greatly appreciated!

0 likes
3 replies
jlrdw's avatar

School student teacher has been done here so many times.

If you Google

site:laracasts.com school student teacher relations

Change the search term if needed.

Explanation, the search here is not as powerful as a Google search.

Snapey's avatar
Snapey
Best Answer
Level 122

Im not sure you can use hasManyThrough because the middle model is a 1:many relationship ?

student ---- school1 ---- teacher1
                     ---- teacher2
        ---- school2 ---- teacher 3
                     ---- teacher 4

I would eager load the relationships

$student->load('schools.teachers');

and then use like this or flatten

Rretzko's avatar
Level 15

Thanks, very much appreciated. I'm following your leads.

Please or to participate in this conversation.