Laravel技巧:使用load、with预加载 区别

Laravel技巧:使用load、with预加载 区别

1、使用load

  1. $posts = Post::all();

  2. $posts->load(
    ‘user’);

2、使用with

$posts = Post::with('user')->all();

懒加载是什么意思呢?

两张表,目录表和教材表。多个教材属于一个目录,那么利用懒加载,你就可以通过先把目录读出来,然后把这些与目录有关的教材一下子读出来完。这样进行数据库读取的次数就少了。

所以我从国外的一个网站上搬来了with和load的用法,大家自行领悟吧。

Both accomplish the same end results—eager loading a related model onto the first. In fact, they both run exactly the same two queries. The key difference is that with() eager loads the related model up front, immediately after the initial query (all(), first(), or find(x), for example); when using load(), you run the initial query first, and then eager load the relation at some later point.

“Eager” here means that we’re associating all the related models for a particular result set using just one query, as opposed to having to run n queries, where n is the number of items in the initial set.

Eager loading using with()

If we eager load using with(), for example:

$users = User::with('comments')->get(); 

if we have 5 users, the following two queries get run immediately:

select * from `users` select * from `comments` where `comments`.`user_id` in (1, 2, 3, 4, 5) 

…and we end up with a collection of models that have the comments attached to the user model, so we can do something like $users->comments->first()->body.

“Lazy” eager loading using load()

In this approach, we can separate the two queries, first by getting the initial result:

$users = User::all(); 

which runs:

select * from `users` 

And later, if we decide(based on some condition) that we need the related comments for all these users, we can eager load them after the fact:

if($someCondition){  $users = $users->load('comments'); } 

which runs the 2nd query:

select * from `comments` where `comments`.`user_id` in (1, 2, 3, 4, 5) 

And we end up with the same result, just split into two steps. Again, we can call $users->comments->first()->body to get to the related model for any item.

Conclusion

When to use load() or with()?

load() gives you the option of deciding later, based on some dynamic condition, whether or not you need to run the 2nd query.

If, however, there’s no question that you’ll need to access all the related items, use with().

 



先说说 关联查询:我们在 Model 类里定义的关联,我们不一定在第一次查询就全部查出来,我们可以在需要的时候再去查询 ,使用 load 方法,可以实现这个目标,
但是这个 load 只是针对单个 model 对象的
如果我们 Model::xxx()->xx() 链式操作最后是 get(),我们得到的结果将会是一个 Collection 实例,
最后调用的方法是 first() 或其他类似的 firstOrFail() 的时候,返回的才是一个 Model 实例
也就是说 load 方法只针对 Model 实例。如下:

总结:with 一步等于执行了两步,
load 分开执行两步

参考:https://www.cnblogs.com/cjjjj/p/9839725.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/112398.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【python】详解pandas库的pd.merge函数「建议收藏」

    【python】详解pandas库的pd.merge函数「建议收藏」pandas.DataFrame.mergepd.merge(left,right,how=’inner’,on=None,left_on=None,right_on=None,left_index=False,right_index=False,sort=True,suffixes=(‘_x’,’_y’),copy=True,indi…

    2022年4月28日
    62
  • 操作系统之——银行家算法C语言实现

    操作系统之——银行家算法C语言实现//银行家算法.cpp:定义控制台应用程序的入口点。//#include”stdafx.h”#include”string.h”#include”stdlib.h”#defineMAX_PROCESS10//进程数上限#defineMAX_RESOURCE_KIND10//资源种类上限#defineMAX_RESOURCE_NUM20 //每种资源可用

    2022年6月2日
    34
  • 达人评测小米平板5怎么样[通俗易懂]

    达人评测小米平板5怎么样[通俗易懂]小米平板5系列将推出三款新机,均会搭载高通处理器,分别为骁龙870、骁龙860和骁龙768G,分别对应高、中、低三个档位,无论是学习还是娱乐、工作,小米平板5都能提供匹配的体验。骁龙870大家此前已经非常熟悉,目前市面上已经有多款搭载该芯片的产品亮相,而骁龙860目前还未在国内上市。据悉,骁龙860处理器是此前骁龙855Plus的增强版,采用7nm工艺打造,CPU主频为2.96GHz,为1+3+4的三丛集架构设计,超大核为Kryo485,并且在5G、内存等和方面带来了全新提升,性能更..

    2022年8月10日
    17
  • rc522优点_51单片机rc522接线图

    rc522优点_51单片机rc522接线图公司需要做刷卡模块,因此选了RC522做demo程序。下面就RC522知识做简要的总结。本人使用stm32的硬件spi接口搭建工程,相关的配置如下:spi配置:引脚配置SDA——-PA4SCLK—-PB13MOSI——-PB15MISO——PB14IRQ—没接RST—-PB0//PB12–NSSPB13–SCKPB14–…

    2022年9月19日
    2
  • debian源代码_debian10软件源

    debian源代码_debian10软件源debhttp://security.debian.org/squeeze/updatesmaindeb-srchttp://security.debian.org/squeeze/updatesmaindebhttp://mirrors.163.com

    2022年10月19日
    2
  • pycharm调试和运行的区别_pycharm调试debug入门

    pycharm调试和运行的区别_pycharm调试debug入门pycharm运行代码时,启动的是测试方案https://blog.csdn.net/qq_41810188/article/details/107359647

    2022年8月27日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号