Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Armaan Dhanji
Armaan Dhanji
4,664 Points

Need help with Eloquent Relationships for my first laravel application attempt

Trying my first Laravel project, a simple dummy "Attendance" application (Like the attendance offered in a small cram school).

My desired behaviour is: The User enters the app and sees a list of classes. Selecting a class, they should see the days of the week that class is being taught(between Mon-Fri). Then, if they touch one of the days of the week, they should see the sections which belong to that class which are being offerred for that day. And if they choose a specific section (say, section1 from 10am-11am), they should be able to see the students who belong to that section and also the teacher teaching that section.

I'm really confused about how to set this up. My models consist of Student, Teacher, Klass, Section, and possibly DaysOfWeek.

I was thinking something along the lines of: A klass hasmany daysofweek. sections belongto dayofweek. sections havemany students and students havemany sections. section hasone teacher.

Would daysofweek even make sense? I was thinking of it being a table with 7 columns (mon-sun). Any help and direction would be greatly appreciated.

2 Answers

Kevin Korte
Kevin Korte
28,149 Points

Hi Armaan,

Without writing application specific code, I think I can help you get on your way. We'll use this documentation: https://laravel.com/docs/5.3/eloquent-relationships

So walking through your scenario, I like to think about how a user is going to use my product, and what info I'd like to show them. So if a student logs in to look at their classes, a student has many classes. If they select a class, than a class could have many days, and days could have many classes, so you have a many to many relationship between classes and days, which requires a 3rd database table to manage the relationship, which following standard naming conventions this 3rd table would be called class_dayofweek. So than drilling down into a day, to see sections, I assume a day can have many sections, and a section could have many days, so again you'd have a *many to many relationship here, and have another tabled called dayofweek_section, to manage the relationship.

Also a section has many students, and a student belongs to a section, and a teach can have many sections, while a section belongs to a teacher...unless you had multiple teachers for one section, which can happen, I've had two professors for one class before, so in that case this would probably become another many to many relationship where is `section_teacher* table would be needed.

The nice part is, if you find yourself needing another relationship of any kind, you can always add it in. Actually, database migrations are very common, and Laravel has a system to help you manage migrations.

Hopefully, that gets you on your way.

Hi Armaan, this is not my areas of knowledge so I have asked the other MOD's if they could take a look for me :)

Craig