I have the following tables:
lessons
- lessonID
- lessonName
..
sections
- sectionID
- lessonID references lessons.lessonID
..
exercises
- exerciseID
- sectionID references sections.sectionID
..
I have set up the following relations:
Lesson:
public function sections()
{
return $this->hasMany('Section', 'lessonID');
}
Section:
public function lesson() {
return $this->belongsTo('Lesson', 'lessonID');
}
public function exercise() {
return $this->hasOne('Exercise', 'sectionID');
}
Exercise:
public function section() {
return $this->belongsTo('Section', 'sectionID');
}
Now I want to say that a Lesson has many Exercises through Sections, so I tried adding
public function exercises()
{
return $this->hasManyThrough('Exercise', 'Section', 'lessonID', 'sectionID');
}
to the Lessons model and I run
Lesson::with('sections', 'exercises')
gives me an empty array for the exercises. How can I accomplish what I want?
Edit: I'm seeding the Exercise table like so:
<?php
class ExerciseTableSeeder extends Seeder {
public function run()
{
DB::table('exercises')->delete();
Exercise::create(array(
'exerciseID' => 1,
'sectionID' => 2,
...
));
Exercise::create(array(
'exerciseID' => 2,
'sectionID' => 4,
...
));
}
}
?>
Edit 2: Here's my models: http://laravel.io/bin/2Rdl
And here are the query logs: http://laravel.io/bin/vebK
So in short: The Lesson and its Sections are returned fine, but I get an empty array for the Exercises.