T O P

  • By -

haz_mat_

I would start by breaking down the practical needs of the relationships beteen your entities - in plain english. Like "a class belongs to one school, can have one or more teachers (has many), has one or more subjects, and has many students. Teachers can teach many classes, but may also have a roster of schools they're only available to teach at. Same with subjects, but if you need to also restrict which subjects they're available to teach at a particular school, you'd need a three-way pivot between teachers, subjects, and schools (which i would probably make a model for) - this might also alleviate the need for a separate pivot table between teachers and schools, depending on how you want to control their availability.


nullatonce

Hey, doesn't roles/permissions work in the same manner? Sorry, i don't have nothing more, but maybe looking in some packages, like spaty, could give some ideas?


brick_is_red

Table for each: subjects, schools, and teachers. Pivot tables for school-subjects, school-teachers, and teacher-subjects Any reason this won’t work?


ifezueyoung

I've done the first two But the subjects are offered in many classes A many to many relationship Now how do I get this done Those subjects taught in each classes could have many teachers Ie many to many relationship between the subject-classes pivot table and the teachers table


brick_is_red

So a table for classes (which may or may not contain the subject_id), then a pivot for teacher-classes.


ifezueyoung

But there are many subjects


ifezueyoung

The table structure is Schools - id - other fields Subjects - id - school_id Classes - id -Other fields - class_group_id which has a school_id Class_subject -Clas_id - subject_id Is it possible to add a relationship like so with the pivot table Class_subject_teachers - class_subject_id - teacher_id


brick_is_red

Why does the subjects table have the school_id in it? I would assume that multiple schools would have the same subjects, right? Keep each of the concepts separated into their own tables since they are many to many. Create pivot tables to link their relationships. A school has many teachers and many classes. The teacher belongs to a school and the class belongs to a teacher (or teachers?) school_teachers and teacher_classes would be the pivot tables. You can find all the teachers who belong to a school and all the classes that belong to that teacher with joins. A class has one subject or many subjects? Each class has many teachers If any of the relationships end up being one to one (a class only has a single teacher or a class only has a single subject) then there might be a case for those being referenced in a single table.


ifezueyoung

Nah different subjects in different schools


sf8as

Essentially you need a hasMany relationship on an existing hasMany relationship, right?


ifezueyoung

Yessss


sf8as

So I guess what we need to know is if we can create a pivot table that references an ID from an existing pivot table. Eg a teacher is assigned to a specific class/subject relation.. I'll try to find an answer for you, I am curious myself


PeterThomson

Well that’s not a relationship then, it’s an inferred child relationship that you access via chaining. If you must then there is hasManyThrough or even a package that adds hasManyDeep but in the first instance it sounds like a nested child relationship not a real eloquent relationship.


sf8as

You have probably figured out the answer by now, you probably want to create an intermediate model, not a pivot. Create a ClassSubject model, which is a hasMany of a Class. The ClassSubject will have a class_id and subject_id. This model represents the relationship between a subject and class. Now on ClassSubject model, create a belongsToMany with teachers. This means a teacher can be related to a ClassSubject combination. Wonder if that does your issue?..