This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I have 3 content type, course, location and schedule. Course belongs many locations, location also belongs to many courses, but schedule can only belong to one course and one location. I already set up many to many relationship on Course model and Location through a pivot table (course_location). My problem comes with how to do wire up / reference the schedule with the pivot table in model, or I am doing it the wrong way.
**course Table**
id
name
**location Table**
id
name
**course_location Pivot Table**
id
course_id
location_id
**schedule Table**
id
course_id
location_id
time
class Course extends Model
{
public $belongsToMany = [
'locations' => [
Location::class,
'table' => 'course_location',
'key' => 'course_id',
'otherKey' => 'location_id',
'timestamps' => true,
],
];
}
class Location extends Model
{
public $belongsToMany = [
'courses' => [
Course::class,
'table' => 'course_location',
'key' => 'location_id',
'otherKey' => 'course_id',
'timestamps' => true,
],
];
}
Maybe this would help?
https://medium.com/@DarkGhostHunter/laravel-has-many-through-pivot-elegantly-958dd096db
In any case, I think you could add a "schedule_id" in your pivot table and access the schedule relation with this.
mjauvin said:
Maybe this would help?
https://medium.com/@DarkGhostHunter/laravel-has-many-through-pivot-elegantly-958dd096db
mjauvin said:
In any case, I think you could add a "schedule_id" in your pivot table and access the schedule relation with this.
@mjauvin Thanks for the reply, great found of the article. I have rethink the database structure and come up with something new.
Since the Schedule
can only belong to one course_location
combination, I restructure the tables as follow:
**course Table**
id
name
**location Table**
id
name
**schedule Table**
id
course_id
location_id
time
Hope this will help others. Once again appreciate for the effort.
So the schedule table is your pivot table as well, right?
Did you use a custom Pivot class?
If you can share your code, that would be appreciated.
1-5 of 5