Laravel with():此方法的作用是什么? [英] Laravel with(): What this method does?

查看:2206
本文介绍了Laravel with():此方法的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手,正在从事小型应用程序的工作.在Laravel教程中,我阅读了Eloquent模型的 with()方法,但无法理解它的工作原理.

I am new on Laravel and working on small application. In Laravel tutorials, I have read the with() method of Eloquent model but not able to understand it's working clearly.

我看过许多教程来阐明这个概念.有人发表了对 with()方法的少量理解,而有人发表了深刻的理解.在某个地方,有人试图清除以下示例:

I have seen many tutorials to clear this concept. Somebody post small understanding and somebody post deep understanding of with() method. At one place someone trying to clear this below example:

$collection = Model_1::where("year", 2016)->with(array("Model_2"=>function($q){$q->with("Model_3","Model_4");}))->with("Model_5","Model_6")->get()->toArray();  

总体而言,我仍然无法理解这种方法的概念.请任何人可以清除我with()方法的概念并解释以上语句的工作吗?预先感谢.

Overall, still I am not able to understand the concept of this method. Please anyone can clear me the concept of with() method and explain the working of above statement? Thanks in Advance.

推荐答案

方法with()使您急切地加载.

Method with() on Eloquent model enables for you eager loading.

这意味着,当您对某个模型进行请求时,如果需要此数据,则可以对数据库进行其他请求.雄辩的将用加载的数据填充您的模型.

That means, when you do request on some model, you could do other requests to the database, if you need this data. Eloquent will fill out you model with loaded data.

如果您不将一些相关模型包含到模型中,并在之后请求它们,那么Eloquent会将新请求应用于数据库.

If you do not include some related models to your model and will request them after, Eloquent will apply the new request to DB.

您可以在文档中了解有关急切加载的更多信息

You could read more about eager loading in the documentation

这篇关于Laravel with():此方法的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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