Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 3 months 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 7 years ago.
0

Sign in to participate in this thread!

Cloudways

Your banner here too?

alextime alextime alextime Joined 30 Mar 2015

Moderators

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 Laravel.io - All rights reserved.