Schema: Media: id, mediable_id, mediable_type, name
Categories id, parent_id, name,
Articles id, title, description, author_id, and some other meta data fields
articles_categories article_id, category_id,
users id, name, email, pass
Relations:
Articles and Categories: Many to Many
Articles and Media: Polymorphic One to Many
- Categories to Media: Polymorphic One to Many
- Articles to Users: One to One
I tried various approach as suggested by damiani at Laravel - Eager Loading Polymorphic Relation's Related Models, but somehow I am not able to eager-load the media for the articles and categories.
$articles = Categories->with(array('user', 'media','articles' => function($query)
{
$query->where('articles.is_featured', '=', 1)
->where('articles.status', '=', 1)
->where('published_at', '>=', new \DateTime('today'))
->orderBy('created_at', 'desc');
}));
I also tried: $articles = Categories->with('articles', 'articles.media', 'articles.user');