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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • IntelliJ IDEA 常用设置(配置)吐血整理。首次安装必需「建议收藏」

    idea很强大,但很多设置并不是满足,经常一安装就要捣鼓很久,为此吐血整理初始化设置:切换主题、忽略大小写、自定义注释、自定义代码颜色、滚轮调节字体、鼠标悬停提示、打开当前文件目录、添加双引号

    2022年4月14日
    48
  • L3-023 计算图(链式求导+bfs拓扑|dfs)「建议收藏」

    L3-023 计算图(链式求导+bfs拓扑|dfs)「建议收藏」原题链接“计算图”(computational graph)是现代深度学习系统的基础执行引擎,提供了一种表示任意数学表达式的方法,例如用有向无环图表示的神经网络。 图中的节点表示基本操作或输入变量,边表示节点之间的中间值的依赖性。 例如,下图就是一个函数 ( 的计算图。现在给定一个计算图,请你根据所有输入变量计算函数值及其偏导数(即梯度)。 例如,给定输入,,上述计算图获得函数值 (;并且根据微分链式法则,上图得到的梯度 ∇。知道你已经把微积分忘了,所以这里只要求你处理几个简单的算子:加法、减法、乘

    2022年8月8日
    6
  • Stack overflow at line 解决办法(重复引入JS导致)

    Stack overflow at line 解决办法(重复引入JS导致)这几天碰到了个莫名其妙的问题,我在一个TR的onDblClick事件里写了一个window.open(XXX);可是每当我双击这一行的时候总是给我报Stackoverflowatline7这个错误,在网上找了很多方法都不行,后来查看源文件发现我把一个外部JS引入了2次,我本身的jsp引入的一次,我每个jsp都会去引入一个公共的jsp,在公共jsp里面我又引入了一次。后来我把本身的jsp引入

    2022年7月15日
    12
  • 利用FormData对象实现AJAX文件上传功能及后端实现「建议收藏」

    利用FormData对象实现AJAX文件上传功能及后端实现

    2022年2月11日
    46
  • 虚拟机安装VMware Tools仍旧不能复制粘贴的解决方法–共享文件夹

    虚拟机安装VMware Tools仍旧不能复制粘贴的解决方法–共享文件夹我们有时会遇到一个问题,那就是我们可以从主机往虚拟机里复制文件,但是从虚拟机往主机复制文件就不行,鼠标永远在虚拟机内。博主重装很多次VMwareTools都没有用,这时就可以考虑共享文件夹。设置共享文件夹步骤如下:1.打开虚拟机设置,打开选项2.点击添加(A)3.设置主机路径和名称4.下一步之后文件夹共享改为总是启用(E)5.博主这里是G:\虚拟机共享文…

    2022年5月18日
    474
  • h3c 配置ssh登录_H3交换机SSH跳转

    h3c 配置ssh登录_H3交换机SSH跳转1.system-view2.public-keylocalcreatersasshserverenable3.user-interfacevtyvty号(04)authentication-modescheme上述操作是设置一个VTY的,如果允许同时登陆多个,需要登几个就设几个VTY,重复上面俩操作即可protocolinboundall4.q…

    2022年10月9日
    0

发表回复

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

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