laravel with()方法与load()方法 [英] laravel with() method versus load() method

查看:1123
本文介绍了laravel with()方法与load()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的试图了解with()方法和load()方法之间的区别,但并不能真正理解.

I really tried to understand the difference between the with() method and the load() method, but couldn't really understand.

正如我所见,使用with()方法是更好的",因为我渴望加载该关系.看来,如果我使用load(),则加载该关系就像使用hasMany()(或与对象之间的关系相关的任何其他方法)一样.

As I see it, using the with() method is "better" since I eager load the relation. It seems that if I use load() I load the relation just as if I would use the hasMany() (or any other method that relates to the relation between objects).

我弄错了吗?

推荐答案

两者均能达到相同的最终结果-希望将相关模型加载到第一个模型上.实际上,它们都运行完全相同的两个查询.关键区别在于,with()渴望在初始查询(例如,all()first()find(x))之后立即立即加载相关模型.使用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.

此处急切"意味着我们仅使用一个查询即可将特定结果集的所有相关模型关联在一起,而不必运行 n 个查询,其中 n 是初始集中的项目数.

"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.

使用with()

Eager loading using with()

如果我们渴望使用with()进行加载,例如:

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

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

...如果我们有5个用户,则会立即运行以下两个查询:

...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)

...,我们最终得到了一组模型,这些模型的注释附加到用户模型上,因此我们可以执行类似$users->comments->first()->body的操作.

...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.

希望使用load()

"Lazy" eager loading using load()

或者,我们可以将两个查询分开,首先通过获取初始结果:

Or, we can separate the two queries, first by getting the initial result:

$users = User::all();

运行:

    select * from `users`

然后,如果我们决定我们需要所有这些用户的相关注释,我们可以急于在事实发生后加载它们:

And later, if we decide that we need the related comments for all these users, we can eager load them after the fact:

$users = $users->load('comments');

运行第二个查询:

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

...然后我们得到相同的结果,只是分为两个步骤.同样,我们可以调用$users->comments->first()->body进入任何项目的相关模型.

...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.

为什么要使用load()with()? load()使您可以选择是否以后根据某些动态条件决定是否需要运行第二个查询.但是,如果毫无疑问您需要访问所有相关项,请使用with(). (文档还引用了使用load()的缓存优势,但我对此并不熟悉;实际上,我相信load()的结果不可缓存.)

Why use load() vs. 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(). (The docs also reference a caching benefit to using load(), but I'm not familiar with that; in fact, I believe the results of load() are not cacheable.)

这两种方法的替代方法是循环遍历初始结果集并查询每个项目的hasMany()关系.这最终将运行 n + 1 个查询,在本示例中为 6 个查询.无论是先使用with()还是先使用load()进行提前加载,都仅运行 2 个查询.

The alternative to either of these would be looping through the initial result set and querying a hasMany() relation for each item. This would end up running n+1 queries, or 6 in this example. Eager loading, regardless of whether it's done up-front with with() or later with load(), only runs 2 queries.

这篇关于laravel with()方法与load()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆