douba05167 2016-10-22 11:20
浏览 39
已采纳

渴望加载,限制急切负载,懒惰渴望加载

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.

  • 写回答

1条回答 默认 最新

  • douyan6871 2016-10-22 12:19
    关注

    Nutshell:

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

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

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

    Example:

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

    Scenario 1:

    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.

    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):

    Controller:

    $posts = App\Post::all();
    
    return view('posts.index', compact('posts'));
    

    blade file:

    @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'));
    

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

    Scenario 3

    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 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.

    Hope this help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测