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

TimeSocks's avatar

Defining a relationship for a report card system

Hi,

I'm looking to design a report card system purely as a learning tool, but I'm stumped on defining the database relationships. Naturally I have Students and Subjects. Do I need a Grades table as well? How should I define their relationships?

TIA

0 likes
2 replies
thc1967's avatar

If you only care about grades for the intersection of students and subjects, you can put the grade on the student_subject pivot table.

If you have marking periods, it gets a bit more complicated.

TimeSocks's avatar

@thc1967 I tried that, but when I go into tinker and try, say,:

$pupil = App\Pupil::find(1)->with('subjects')->get()

I get an error:

Column not found: 1054 Unknown column 'subjects.pupil_id' in 'where clause' (SQL: select * from `subjects` where `subjects`.`pupil_id` in (1, 2, 3))

EDIT:

Setting both Pupil and Subject to belongsToMany and running:

$pupil = App\Pupil::find(1)->subjects

in Tinker outputs:

$pupil = App\Pupil::find(1)->subjects
=> Illuminate\Database\Eloquent\Collection {#686
     all: [
       App\Subject {#690
         id: 1,
         subjectname: "English",
         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#684
           pupil_id: 1,
           subject_id: 1,
         },
       },
       App\Subject {#687
         id: 2,
         subjectname: "Maths",
         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#685
           pupil_id: 1,
           subject_id: 2,
         },
       },
     ],
   }

This doesn't seem to access the grade that's assigned to each subject in the pupil_subject table though. What am I doing wrong?

Please or to participate in this conversation.