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

fetch404's avatar

How should I structure this?

Hey,

I was wondering how I could structure this. I'm working on a sort of online gradebook/portal, and I can't figure out how I should structure the relations.

Basically:

  • School administrator signs up, and creates a new team account for their school.

    The administrator can invite teachers to the school team.

    Teachers sign up, create their classes, and invite students.

    Now, there are two different portals: teacher, and student. In the teacher portal, teachers can give students individual grades on assignments. Then, in the student portal, the students can see their own grades.

The problem is, I'm not sure how I can keep the grades individual, while avoiding creating "StudentAssignment" and "StudentAssignmentGrade" models.

Is there any way to do this?

0 likes
15 replies
bikerboy's avatar

From your requirements, I see that each grade belongs to an assignment. I would have the following models.

// App/Student.php

class Student extends Model {
    public function assignments() {
        return $this->hasMany('App\Assignment', 'user_id');
    }

    public function grades() {
        return $this->hasManyThrough('App\Grade', 'App\Assignment');
    }
}

One model for the Assignments

// App/Assignment.php

class Assignment extends Model {
    public function grades() {
        return $this->hasMany('App\Grade');
    }
}

and finally a model for Grades

// App/Grade.php

class Grade extends Model {
    public function assignment() {
        $this->belongsTo('App\Assignment');
    }
}

You can access the grades directly from the User/Student model, or you can fetch an assignment and get the grades for that specific one.

ayekoto's avatar

Mind you,

Admin, Teacher, n Student r all User..

So there shouldn't be anything like StudentModel, AdminModel blah blah

ayekoto's avatar

Because looking at it, student , admin, teacher are just roles a user can belong, So pls let structure the relationship well,

Am not too good at structuring too cos that what's am trying to work on improving myself , and am always following relationship discussion to learn

jlrdw's avatar

That can get tricky, a student has the assignment. Later a student wants to view their grade. But you are better of keeping the relations as simple as possible. teacher->student->assignment. Grades isn't a separate table, it could be in students or assignments table depending on how you want it. I wouldn't have a table just for grades.

fetch404's avatar

Never thought that this would turn into a bigger discussion. Thanks for all your help!

Feel free to keep discussing how you would do it :-)

bikerboy's avatar

If each student receives an individual assignment then you don't need a grades table, however I don't think that's the case.

Multiple students will receive the same assignment and each student can receive a different grade for that specific assignment. Therefore a separate table to store the grades for each student for each assignment is required.

1 like
fetch404's avatar

Does anyone know how I could use pivot tables for this? This really puzzles me.

jlrdw's avatar

If you can do this without a pivot table I would once you start getting in the pivot tables things get more complicated. I have done complex relations before and I have never needed a pivot table. Always try to look for a simple solution as possible.

fetch404's avatar

Thanks, everyone. I'll post pictures when it's working!

Please or to participate in this conversation.