Q:

Nested Eager Loading Specific Columns

when I do this
$category = NewsCategory::whereSlug($slug)->with('news.category:id,title,slug')->firstOrFail();

then I have following queries
select * from `news_categories` where `slug` = 'novinki' limit 1
select * from `news_posts` where `news_posts`.`category_id` in (2) and `news_posts`.`deleted_at` is null
select `id`, `title`, `slug` from `news_categories` where `news_categories`.`id` in (2)

but I want to load specific columns on first news relation also and I have error if I do like this
$category = NewsCategory::whereSlug($slug)->with('news:id,title,image.category:id,title,slug')->firstOrFail();

what is the correct way to get this query?
select * from `news_categories` where `slug` = 'novinki' limit 1
select `id`, `title`, `image` from `news_posts` where `news_posts`.`category_id` in (2) and `news_posts`.`deleted_at` is null
select `id`, `title`, `slug` from `news_categories` where `news_categories`.`id` in (2)```

A:

I would put the eager loaded relationships into an array
Try this:
$category = NewsCategory::whereSlug($slug)
              ->with([
                  'news:id,title,image,category_id,deleted_at',
                  'news.category:id,title,slug'
              ])->firstOrFail();

Or as my personal preference, use closures to expand the query like this:
$category = NewsCategory::whereSlug($slug)
              ->with([
                  'news' => function($query){
                      $query->select('id','title','image','category_id','deleted_at');
                   },
                  'news.category' => function($query){
                      $query->select('id','title','slug');
                   }
              ])->firstOrFail();

