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

laravel_enthusiast's avatar

Laravel 8 Students - Majors - Marks relationship implementation

Hello everyone!

I am working at a high school like project.

So far, I have the following related entities in my project: Classes, Groups, Majors, Students, Teachers.

The relations between the mentioned models are as follows: I will use the following format in parenthesis for a better understanding ( Models involved / Relationship / Explanation )

  1. A class has many groups <-> A group belongs to one class ( Classes - Groups / one to many / You can create groups and attach them to one class )
  2. A major belongs to many groups <-> A group belongs to many majors (Majors - Groups / many to many / When a group gets created, the user will need to select the majors that will be studied in that group from the existing majors )
  3. A group has many students <-> A student belongs to one group ( Groups - Students / one to many / A student can be added to a specific group )
  4. A group belongs to many teachers <-> A teacher belongs to many groups ( Groups - Teachers / many to many / Each teacher can teach his specific major only in groups in which his major is studied and has no teacher )

My issue: I want to implement a new marks model. I don't have an idea of how should I link the students specific majors inherited from the groups they are in to the marks.

Example:

  1. We create class "1".
  2. We create majors: "Mathematics", "Physics", "Chemistry"
  3. We create group "A" linked to class1 in which "Mathematics", "Physics", "Chemistry" majors are studied
  4. We create a new student and we assign him to group "A". Observation: Right now, if I'll dd(student->group->majors) I will get all of the majors that are studied in that student's group.

How should I implement the marks relationship? I want to be able to create/read/update/delete a mark for each major of a specific student.

Let's say I have Student "John" in the "A" group. The admin of the page should assign marks for John's performance at the majors that are studied in John's group "Mathematics", "Physics", "Chemistry".

Thank you in advance for your answers!

0 likes
2 replies
cjholowatyj's avatar

This sounds like a complicated web of objects!

I already wrote a much more elegant response to this post, but the page expired and deleted my response before I could submit it :(

It sounds like you need a GroupMajors object which represents the relationship between Groups and Majors, since the Marks sound like they need to be associated with both at the same time. Since a GroupMajors instance would have one Group and one Major, and each Group has only one Class, you could define a relationship between GroupMajors and your new Marks class, and then you have that link between Marks and Classes, which I assume is the intended result. Then after creating those two classes, I'd probably spend some time creating those accessor functions that make it easier to traverse the complex relationships so you can call $student->classes() instead of needing to fetch groups in every call then fetch the Classes for those Groups... and also something like $student->getClass(~class_id~)->marks() would be much more logically cleaner than fetching the group for a student, fetching the classes for each of those groups, and then throwing majors into the mix...

Or would it be more appropriate to say that each Class has one or many Majors, and a Group could be a collection of those ClassMajors relationships? (i.e. a Group has one or many ClassMajors, and a ClassMajor belongs to one Class and a ClassMajor belongs to one Major) That way if an instructor wanted to analyze data pertaining to all "Marks for all Chemistry Majors in Course X" they could analyze that more cleanly and the Group object could be more of an abstract concept rather than a contingent one if that makes sense...

Not saying that by responding to your post that I completely understand what you're going for here... I hope I was close, or at least sparked some ideas!

laravel_enthusiast's avatar

Hello!

Thank you very much for your effort.

"It sounds like you need a GroupMajors object which represents the relationship between Groups and Majors, since the Marks sound like they need to be associated with both at the same time."

I already have a GroupMajors relationship set. This way, a new group created already has x,y,z majors assigned to it by using many to many relationship.

I think this is the normal flow in a school. You have multiple classes in which you can have multiple groups in which different majors can be studied. In those groups we'll insert students.

What I really need is a way of transfering the majors assigned to the group to the student that is assigned to that group. All students that are part of a group will study only the majors that are linked to that group. This way, I want to be able to offer marks for each student for each major.

If I have a new marks table in my db, each mark should be linked to a student by major.

Example: Student "John" can have marks 3, 4, 5 but 3 is for "Physics", 4 is for "Chemistry" and 5 is for "Mathematics".

Please or to participate in this conversation.