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

ghischa's avatar

hasMany on pivot table

I had 4 tables one teachers one schools one students and the pivot table school_teacher. Many teacher can work on many school and many school has many teacher in the school_teacher pivot table i have two attribute cycle and grade. Also a student may have many teacher and many classroom so i put a relation between student belongsTo school_teacher and a hasMany on school_teacher. The problem it's i wan t from teacher view see the list of student for one teacher.

0 likes
8 replies
tykus's avatar

I don't understand the relationships you are describing.

What exactly forms the basis of the relationship between the school_teacher and student?

I think you might be missing a pivot to describe a relationship between a student and school for example, because I don't see where the student is represented in the school_teacher table.

Can you give a little more detail e.g. schema, what problem you want the application to solve?

ghischa's avatar

Sorry i'm french is hard for me to explain in english. I want to associate student with classroom because in high school kids have many teacher and many classroom( the grade and cycle in school_teacher)

Basic exemple: table teacher name=John Doe table school name: Longon school

school_teacher id = 1 grade = 3rd teacher_id=1 school_id=1

student table id =1 name= Jeanne Doe school_teacher =1

in this exemple jeanne doe have john doe has a teacher in longon school she's in grade 3.

hope is more clear for you thank for you're help this way i thing i can on teacher view see the school and the students one teacher.

ohffs's avatar

I think you will need a school_student table to do what you want? If one teacher can be at different schools?

tykus's avatar
tykus
Best Answer
Level 104

Maybe you might benefit from renaming the school_teacher pivot -- the fact that you have extra columns beyond the school and teacher ids suggests that it is an entity in its own right within the context of your application. I wonder if the data model needs to be more complex than you have suggested.

Are you writing a school enrollment system, or a school timetable system; if you Google Entity Relationship Diagrams with both of these terms it might help you to understand how to identify these additional entities and relationships.

Of course, come back and pick our brains again if this not helping!!!

ghischa's avatar

Thanks for you're help I will not hesitate if i have a other question thank again

ben_s247's avatar

By the way make sure you are referencing the relationship correctly. A student may have many teachers and A teacher may have many students. so it's a many to many relationship and requires a pivot table. in the model this should look like

class Teacher extends Model
{
    protected $table = 'teachers';

    public function students()
    {
        return $this->belongsToMany(Student::class);
    }
}

Then in the Student Model it should look similar to this

class Student extends Model
{
    protected $table = 'students';

    public function teachers()
    {
        return $this->belongsToMany(Teacher::class);
    }
}

this allows you to get the relationship. for example

$teacher = Teacher::find(1);
$teacher->students()

or if your wanting to display all students and their relationships

$teachers = Teacher::with('students')->get();

then use a foreach loop and reference the relationship

foreach($teachers as $teacher){
    dd($teacher->students());
}

note this relationship is Belongs to Many both ways. if you put data into your pivot table you should read up about just query it using

$teacher = Teacher::find(1);

foreach($teacher->students() as $student){
    echo $student->pivot->fieldname;
}

for more info have a look at eloquent relationships here: http://laravel.com/docs/5.1/eloquent-relationships#many-to-many hope that helps

ben_s247's avatar

oh and with the schools thing the question to ask is can One teacher work at Many Schools and can Many Schools have the One Teacher defining a many to many relationship.

kunaldodiya's avatar

I am also trying to apply the same, I am not sure how to, need some help, is it okay to have belongsToMany and the reverse relationship of hasMany ?

For Example, A plan hasMany Features and A Feature belongs to many Plans ???

Please or to participate in this conversation.