Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

All you need to do is create two different pivot tables. One for Tests and one for Modules. So your Courses model contains a relationship of "belongsToMany('App\Test')" as well as "belongsToMany('App\Modules')" for tests() and modules() respectively. Your two pivot tables would be named course_test and course_module and contain the correct columns.

At anytime you can query a courses Tests or Modules by first getting a course (for ex $course = App\Course:first()), then you can get tests by $course->tests and modules by $course->modules if you've set the relationship up correctly.

Let me know if you need more specific code or you get the general idea of what I'm saying.

There's also no need to create Eloquent models for pivot tables. If you set up the relationships correctly Eloquent is smart enough to go and find that pivot table, it does use a naming convention that you have to use to make it work out of the box. You can specify the pivot columns though... @JeffreyWay has a great video on this: https://laracasts.com/series/laravel-5-fundamentals/episodes/21

Luke

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

alextime alextime Joined 30 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.