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

NoLAstNamE's avatar

Eloquent relationship

I'm confused about setting the eloquent relationship and the table in my database. I have a Teacher which is saved in the users table with the role 1, and a Student which is also saved in the users table but with the role of 2.

The student belongs to a section and in the student dashboard, I will show who is the Adviser of that section he/she belongs to.

Currently, I have the sections table, every section has an Adviser, and every section can only have one adviser.

I'm thinking of creating section_user, is this the right approach?

// section_user
+------+-------------+----------------+
|  id  |   user_id   |   section_id   | 
+------+-------------+----------------+
|  1   |      1      |        2       |
+------+-------------+----------------+
// users
+------+-----------+----------+----------------+
|  id  |   name    |   role   |   section_id   | 
+------+-----------+----------+----------------+
|  1   |  teacher  |     1    |      null      |
|  2   |  student  |     2    |       2        |
+------+-----------+----------+----------------+
// sections
+------+-------------+
|  id  |     name    |
+------+-------------+
|  1   |  Section A  |
|  2   |  Section B  |
+------+-------------+
0 likes
5 replies
Sinnbeck's avatar

@benjamin1509 you will need something to determine that. You could add an advisor_id to the sections table.

1 like
NoLAstNamE's avatar

@Sinnbeck I will add advisor_id column to the sections table, so when assigning a teacher to a section save the user_id and section_id to section_user table, and update the sections table column advisor_id.

Please or to participate in this conversation.