Laravel-嵌套的渴望加载

时间:2018-09-28 06:57:06

标签: laravel laravel-5 eloquent

this section中,我读到有关嵌套的渴望加载的信息

但此情况未在其中显示。

我可以代替

$books = App\Book::with(['author.posts', 'author.comments', 'author.rating'])->get();

这样吗?

$books = App\Book::with(['author' => function ($query) {
    $query->with(['posts', 'comments', 'rating'])
}])->get();

我可以提出这样的请求吗?

$books = App\Book::with(['author' => function ($query) {
    $query->with([
        'posts' => function ($query) {
            $query->with('author');
        }, 
        'comments' => function ($query) {
            $query->with(['author'  => function ($query) {
                $query->where('created_at', '>=', now()->subDays(5));
            }]);
        }
    ]);
}])->get();

0 个答案:

没有答案