渴望加载,约束渴望加载,懒惰渴望加载 [英] Eager Loading, Constraining Eager Loads, Lazy Eager Loading

查看:88
本文介绍了渴望加载,约束渴望加载,懒惰渴望加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Laravel,并且正在使用Laravel 5.2.我想了解急切加载,约束急切加载和懒惰快切加载.这三个之间的异同是什么?您能为每个例子举个例子吗?我已经阅读了Laravel文档,但是我还是不明白.希望您能给出更清晰的解释.谢谢.

I'm learning Laravel, and I'm using Laravel 5.2. I want to know about Eager Loading, Constraining Eager Loads, and Lazy Eager Loading. What are the similarities and differences between those three? Can you give an example for each of them? I have read Laravel documentation, but I still don't understand. I hope you can give clearer explanation. Thanks.

推荐答案

果壳:

Eager Loading使您可以有效地获取某些模型的关系.

Eager Loading allows you to get the relationships for some Models efficiently.

Constraining Eager Loads再次提高了效率,但是您可以限制结果,例如日期范围,特定ID等

Constraining Eager Loads Again makes it efficient but you can limit your results e.g. date range, specific id etc

Lazy Eager Loading适用于已加载父模型的时间

Lazy Eager Loading Is for when you already have your parent models loaded

示例:

好吧,可以说您正在创建一个博客,其中将有帖子,而那些帖子可以有评论和顶.

Ok, lets say you're making a Blog where you will have Posts and those post can have comments and likes.

方案1:

您想获取所有Posts及其CommentsLikes.您会加载所有Posst,然后遍历它们并到达commentslikes吗?可以,但是这样做可能会变得非常昂贵,因为它可能最终会执行许多查询.或者,您可以加载帖子,然后获取它们的id,并加载这些id所需的所有评论,并且对于喜欢的评论也是如此.本质上,这就是Laravel对急切加载的作用.

You want to get all the Posts with their Comments and their Likes. Would you load all the Posst and then loop through them and get there comments and likes? Well, you could but this could end up being very expensive as it could end up performing many, many queries. Or you could load the posts and then get their ids and load all the comments that you need for those ids and the same for there likes as well. This is essentially what Laravel does with eager loading.

方案2(方案1的真实示例):

Scenario 2 (a real world example of Scenario 1):

您正在为帖子创建提要.因此,您已经加载了所有帖子,然后想要显示它有多少喜欢和评论,这样您将获得(非常基本的)信息:

You're creating you feed for the posts. So, you've loaded up all your Posts and then you want to show how many likes and comments it has so you would have something like (very basic):

控制器:

$posts = App\Post::all();

return view('posts.index', compact('posts'));

刀片文件:

@foreach($posts as $post)

    <h2>{{ $post->title }}</h2>

    <p>{{ $post->description }}</p>

    <p>
        Likes: {{ $post->likes->count() }} <br>
        Comments: {{ $post->comments->count() }}
    </p>

@endforeach

上面的方法可以工作,但是对于每个循环,它实际上都是在查询数据库.将您的控制器更改为:

The above would work but for every loop it would actually be querying the database. Changing your controller to:

$posts = App\Post::with('likes', 'comments')->get();

return view('posts.index', compact('posts'));

然后将预先保存所有Postscommentslikes,并保存数据库并提高应用程序的效率.

Will then get the comments and likes for all of the Posts beforehand saving the database and making your application much more efficient.

场景3

我想显示Posts,但我只想显示制作的最后3个comments.

I want to show the Posts but I only want to show the last 3 comments that were made.

$posts = App\Post::with(['comments' => function ($query) {

        $query->limit(3);

    }]);

Lazy Eager Loading适用于已经加载了Posts的情况,然后您需要在获取事实后获取所有的commentslikes.原理相同,但是您将使用load而不是with.可能使用负载的另一个原因是,如果对控制器使用Route model binding,则Post将已经被检索到,但是您仍然希望获得它的关系.

Lazy Eager Loading is for when you've already loaded your Posts and you then need to get all the comments or likes after the fact. Same principle but you would use load instead of with. Another reason you might use load is if you using Route model binding for your controllers so the Post will have already been retrieved but you still want to get it's relationships.

希望获得帮助!

这篇关于渴望加载,约束渴望加载,懒惰渴望加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆