If a user can have multiple sections and sections can have multiple users, then it is correct. It's called a many to many relationship https://laravel.com/docs/9.x/eloquent-relationships#many-to-many
Apr 2, 2022
5
Level 8
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 |
+------+-------------+
Level 102
1 like
Please or to participate in this conversation.