嵌套3深时,在laravel查询上急于加载 [英] Eager Loading on laravel query when nested 3 deep

查看:92
本文介绍了嵌套3深时,在laravel查询上急于加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Laravel网站上有Jobs and Projects,它们之间有关系.我正在尝试减少查询的数量(并注意N + 1问题).我这样做是为了在输出JobItems时渴望在控制器中加载.

I have Jobs and Projects in my Laravel site and there is a relationship between them. I'm trying to reduce the number of queries made (and be mindful of N+1 problem). I do this with eager loading in my controller when outputting my JobItems.

在嵌套时如何执行此操作:JobItems> Project> JobItems.

How can I do this when I nest: JobItems > Project > JobItems.

在一种情况下会发生这种情况,而我却得到了重复的查询.我不确定如何渴望加载.我将概述这种情况:

There is one instance where this happens and I am getting duplicate queries. I'm not sure how to eager load. I'll outline the situation:

每个JobItem都有一个小时列.

Each JobItem has a column for hours.

在我的项目中,我对所有关联的JobItem小时进行求和,以确定项目中的总时数. (例如,每个有4个小时的3个JobItem,然后在我的项目模型中有一个访问器,显示为projectHours = 12).

In my project I SUM all associated JobItem hours to determine the total hours in a project. (Eg. 3 JobItems with 4hours each, I then have a an accessor in my Project Model that says projectHours = 12).

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