Table Structure
chapters
id
name
chapter_items
id
course_id
itemable_id
intemable_model
lectures
id
name
quizes
id
name
Models
class Course extends Model
{
public function chapters()
{
return $this->hasMany('App\Model\Chapter');
}
}
class Chapter extends Model
{
public function items()
{
return $this->hasMany('App\Model\Chapter');
}
}
class ChapterItem extends Model
{
public function itemable()
{
return $this->morphTo();
}
}
class Quizes extends Model
{
public function comments()
{
return $this->morphMany('App\Models\ChapterItem', 'itemable');
}
}
class Video extends Model
{
public function comments()
{
return $this->morphMany('App\Models\ChapterItem', 'itemable');
}
}
Retrive items
Course::find($id)->with('chapters.items.itemable')->first()
Docs https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations