I want to make a relation between teacher, class, and student. A teacher has only one class and each class have many students. My question is that which relation I use Laravel to create this relation?
Asked
Active
Viewed 189 times
-3
-
Are you wanting to access students straight from the teacher model? Or access them in order from teacher->class->students ? – Andy Nov 02 '21 at 16:39
-
^ You can do both of those things in Laravel, with the same Models and Relationships. @SheikhHammad, what is the actual question here? Have you tried setting this up and had difficulty? If you're looking for a recommended Database structure, then maybe https://dba.stackexchange.com/ is a better place to ask. If you're looking for us to write the Migrations, Models and Relationships for this, then you're in the wrong place, as Stackoverflow is not a free coding service. – Tim Lewis Nov 02 '21 at 16:42
-
@Andy, I want to access them in order – Sheikh Hammad Nov 02 '21 at 16:53
-
@SheikhHammad, Ok. Can you share the relationship code in each of those models? It's hard to know what might be going wrong without seeing what you have attempted already. You might want to take a look at this: [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Andy Nov 02 '21 at 16:59
-
Before you use Laravel, you should understand about database relations first. If you use Laravel without basic, later you will be confused without Laravel. – Wahyu Kristianto Nov 02 '21 at 17:00
1 Answers
0
You have to read eloquent relationships doc to find the solution : https://laravel.com/docs/8.x/eloquent-relationships
Some tips :
A teacher can have only one class so it is a one to one relation : https://laravel.com/docs/8.x/eloquent-relationships#one-to-one
Classes can have many students so it is a one to many relation : https://laravel.com/docs/8.x/eloquent-relationships#one-to-many
if students can belong to many classes, it's not an one to many relation but a many to many relation : https://laravel.com/docs/8.x/eloquent-relationships#many-to-many-polymorphic-relations
If you want more help, don't hesitate to ask me !
(i am french so sorry if my english is bad)

Hugo
- 1