"with"与"with"有什么区别?和“加载"在拉拉韦尔 [英] what is the different between "with" and "load" in laravel

查看:111
本文介绍了"with"与"with"有什么区别?和“加载"在拉拉韦尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了laravel文档,在查询中没有发现 With Load 之间的区别,在哪种情况下我们需要使用With或Load?请描述一下

I have go through the laravel documentation, I didn't get the different between With or Load in queries, In which condition we need to use With or Load? Please describe it

Model::find(1)->with('firstModel','SecondModel');

Model::find(1)->load('firstModel','SecondModel');

推荐答案

Model::find(1)->with('firstModel','SecondModel');

根本没有做任何事情,因为with是一个新生成器,与从find(1)返回的模型无关.

Isn't really doing anything at all, as with is a new builder now that isn't related to the model returned from find(1).

Model::with(....)->find(1);

将渴望加载关系.

Model::find(1)->load(...);

是懒惰的渴望加载.它将关系加载到您已经获取的现有Model实例中. (因为find(1)返回一个Model实例,而load告诉它然后加载指定的关系) load也可以在Collections上使用,以加载所包含的所有Model实例上的关系.与单个模型相比,load在处理一组模型时更有用,因为仅对模型上的关系使用动态属性(延迟加载,如果尚未加载),将导致运行相同的查询.在集合上,它消除了在迭代过程中出现的所有N + 1问题.

Is lazy eager loading. It is loading the relationships to an existing Model instance that you already have retrieved. (as find(1) is returning a Model instance and load is telling it to then load the relationships specified) load can also be used on Collections to load relationships on all the Model instances contained. load is more useful when dealing with a set of Models compared to a single one as just using the dynamic property (lazy loading, if not loaded already) for a relationship on a Model would cause the same queries to be ran. On a set it removes any N+1 issue while iterating through them.

您可以查看这篇文章,以获取有关此内容的更多信息.

You can check out this article for some more information on this.

asklagbox-博客-雄辩的查询关系

这篇关于"with"与"with"有什么区别?和“加载"在拉拉韦尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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