Support the ongoing development of →
Database Eloquent
Last updated 3 months ago.

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:


Last updated 7 years ago.

Sign in to participate in this thread!


Your banner here too?

alextime alextime alextime Joined 30 Mar 2015


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

Your logo here?

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

© 2022 - All rights reserved.