如何急切加载laravel中的嵌套关​​系?

How to eager load the nested relationship in laravel?

我有三个实体用户、博客和评论。我可以使用以下代码加载用户博客:

$userBlogs = User::with('blogs')->get();

但是我怎样才能预先加载与每个博客相关的评论呢?

用户:

public function blogs() {
    return $this->hasMany(Blog::class);
}

博客:

public function comments() {
  return $this->hasMany(Comments::class);
}

你可以做到

$userBlogs = User::with('blogs.comments')->get();

或者你可以

$userBlogs  = User::with(['blogs' => function ($query) {
    $query->with('comments');
}]