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


相关推荐

  • SICP 1.20经验

    SICP 1.20经验

    2021年12月17日
    42
  • 算法学习路线总结

    算法学习路线总结1.基础算法总结点击就可以查看相关博客文档讲解CreatedwithRaphaël2.2.0基础算法排序、查找算法选择排序冒泡排序插入排序

    2022年6月19日
    27
  • view.performClick()触发点击事件

    view.performClick()触发点击事件view.performClick(),主动触发点击

    2022年6月16日
    74
  • 网络python培训班「建议收藏」

    网络python培训班「建议收藏」为进一步推动高等院校人工智能教学工作的开展,加强国内各高等院校同行间的交流,培养国内的师资力量,将人工智能最新实训内容带入课堂,特举办“人工智能系列课程理论与实践”高级培训班。该培训定于2021年7月20日开始,共包含七大专题,每个专题5天左右,共计30天,通过线上直播的方式进行集训。七大专题分别为Python机器学习,图像识别与深度学习,深度学习与NLP,知识图谱、图神经网络和强化学习,深度学习PyTorch理论与实战。本次培训由权威专家主讲,提供实验环境及实验数据,并提供配套资料,通过剖析工程案例展

    2022年7月19日
    20
  • 【Oracle错误】unable to connect 08004 ora12154

    【Oracle错误】unable to connect 08004 ora12154从网上得知,oracle有个bug:连oracle的程序exe的路径中如果包含(、还有啥特殊字符的时候,就连不上,报错误ora12154。我今天也遇到了,我在EA连oracle的时候,死活要报这个错,后来把EA的安装目录从C:\ProgramFiles(x86)中剪贴到没有()的地方,就可以了。参考链接:http://www.2cto.com/database/201

    2022年7月24日
    16
  • php属于前端还是后端_php实时推送到前端

    php属于前端还是后端_php实时推送到前端功能说明使用第三方平台goeasy实现服务端向前端推送数据基本原理WebSocket使用准备申请goeasy账号并创建应用官网http://www.goeasy.io安装并开启goeasy插件(注意清除缓存)在插件配置中填写应用的Appkeys等配置项使用说明使用插件集成的事件插件在前台(index模块)和后台(admin模块)各集成了两个默认的事件订阅,可以在js中通过监听top来处理,例:也…

    2025年5月27日
    4

发表回复

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

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