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)
上一篇 2021年11月8日 下午2:00
下一篇 2021年11月8日 下午3:00


相关推荐

  • dropdownlist控件的几个属性selectedIndex、selectedItem、selectedValue、selectedItem.Text、selectedItem.value的区别…

    dropdownlist控件的几个属性selectedIndex、selectedItem、selectedValue、selectedItem.Text、selectedItem.value的区别…1.selectedIndex——指的是dropdownlist中选项的索引,为int,从0开始,可读可写2.selectedItem——指的是选中的dropdownlist中选项,为ListItem,只读不写3.selectedValue——指的是选中的dropdownlist中选项的值,为string,只读不写4.selectedItem.Text——指的是选中的drop…

    2022年10月16日
    4
  • 内网渗透-域管理员定位

    内网渗透-域管理员定位目录 Part1 内网域渗透基础概念 1 什么是域 1 权限管理比较集中 管理成本降低 2 保密性加强 3 安全性加强 4 提高了便捷性 2 域渗透思路 Part2 域管理员定位基础 Part3 域管理员手动定位 Part4 自动化收集时常用的域管理员定位工具 SPN 扫描 psloggedon exePVEFindAD exenetview exeName 的 NSE 脚本 PowerView PS1 脚本 Part1 内网域渗透基础概念 1 什么是

    2026年3月19日
    3
  • vc怎么改变背景颜色_vc运行界面怎么设置颜色

    vc怎么改变背景颜色_vc运行界面怎么设置颜色最近眼睛发炎,特别怕亮色,看到vc的开发环境都太亮,于是想修改。1>在菜单”Tools”->”Options”的最后一页”Format”中选择“sourcewindow”,将前景色改为黑色,将背景色改为淡灰色。2>改变系统的窗口背景色.设置方法:桌面右击属性选择外观

    2022年8月12日
    51
  • mux-vlan原理_三层交换机配置实例

    mux-vlan原理_三层交换机配置实例学网络,就在IE-LAB国内高端网络工程师培养基地MUXVLAN(MultiplexVLAN)提供了一种通过VLAN进行网络资源控制的机制。通过MUXVLAN提供的二层流量隔离的机制可以实现企业内部员工之间互相通信,而企业外来访客之间的互访是隔离的。为了实现报文之间的二层隔离,用户可以将不同的端口加入不同的VLAN,但这样会浪费有限的VLAN资源。采用端口隔离功能,可以实现同-V…

    2026年1月25日
    4
  • CSS中设置鼠标样式

    CSS中设置鼠标样式cursor规则是设定网页浏览时用户鼠标指针的样式,也就是鼠标的图形形状所有主流浏览器都支持cursor属性。注释:Opera9.3和Safari3不支持 url 值。注释:任何版本的InternetExplorer(包括IE8)都不支持属性值"inherit"。定义和用法cursor属性规定要显示的光标的类型(形状)。该属性定义了鼠标指针放在一个…

    2022年5月31日
    46
  • SparkSQL(一)

    SparkSQL(一)简介 spark1 0 版本就已经退出 SparkSQL 最早叫 sharkShark 是基于 spark 框架并且兼容 hive 执行 SQL 执行引擎 因为底层使用了 Spark 比 MR 的 Hive 普遍要快上两倍左右 当数据全部 load 到内存中 此时会比 Hive 快上 10 倍以上 SparkSQL 就是一种交互式查询应用服务特点 1 内存列存储 可以大大优化内存的使用率 减少内存消耗 避免 GC 对大量数据性能的开销 2

    2026年3月16日
    3

发表回复

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

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