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


相关推荐

  • redis如何设置定时过期_redis 设置过期时间[通俗易懂]

    redis如何设置定时过期_redis 设置过期时间[通俗易懂]1、设置过期时间功能:即对存储在redis数据库中的值可以设置一个过期时间。作为一个缓存数据库,这是非常实用的。如我们一般项目中的token或者一些登录信息,尤其是短信验证码都是有时间限制的,按照传统的数据库处理方式,一般都是自己判断过期,这样无疑会严重影响项目性能。我们setkey的时候,都可以给一个expiretime,就是过期时间,通过过期时间我们可以指定这个key可以…

    2022年9月26日
    2
  • 分页和多条件查询功能

    分页和多条件查询功能

    2022年1月5日
    42
  • MFC:进程间通信方式[通俗易懂]

    MFC:进程间通信方式[通俗易懂]windows下进程间通信方式:1、剪贴板(Ctrl+C;Ctrl+V)2、匿名管道3、命名管道4、邮槽/****************************************************************************************/一、剪贴板:voidCClipboardDlg::OnBtnSend()//向剪贴板中放置

    2022年10月11日
    4
  • redis之淘汰策略和删除策略_局部淘汰策略

    redis之淘汰策略和删除策略_局部淘汰策略redis内存不足时的淘汰策略一般情况下,当内存超出物理内存限制时,内存数据将与磁盘产生频繁交换(swap),swap会导致redis性能急剧下降,对于访问量较大的情况下,swap的存取效率会让服务基本处于不可用的状态。在生产环境中,一般不允许redis出现swap行为,redis提供了maxmemory设置其最多可占用的内存空间。当redis使用的内存超出maxmemory时,此时已经没有多余可用的内存空间,新的数据将无法写入,redis提供了几种数据淘汰策略,用于清理数据,腾出空间以继续

    2022年10月20日
    2
  • mysql安装教程csdn_安装配置mysql教程

    mysql安装教程csdn_安装配置mysql教程一、下载地址:https://dev.mysql.com/downloads/mysql/当前最新是8.0版本,我选择上一个最新的mysql-5.7.24-winx64.zip二、安装MySQL安装文件分两种.msi和.zip,.msi需要安装zip格式是自己解压,解压缩之后其实MySQL就可以使用了,但是要进行环境变量配置zip格式是自己解压我的电脑->属性->高级->环境…

    2025年9月7日
    8
  • scrollHeight的分析[通俗易懂]

    scrollHeight的分析[通俗易懂]<!DOCTYPEhtmlPUBLIC”-//W3C//DTDXHTML1.0Transitional//EN””http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”><htmlxmlns=”http://www.w3.org/1999/xhtml”><head>&…

    2022年7月24日
    10

发表回复

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

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