如何在多层次上加快关系? [英] How to eager load relationships on multiple levels?

查看:133
本文介绍了如何在多层次上加快关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:



ProductA - ProductB - 功能 - 选项



我有以下关系:



ProductA belongsToMany ProductB
ProductB属于多个功能
功能belongsToMany选项



当我想看到ProductA的详细信息(一个有ProductA id的帖子,将由一个控制器管理)我想要加载所有的关系
将一个包含所有细节的变量传递给视图。 / p>

是否可以通过单一的雄辩指令?



这样的事情(我知道它不工作):是它是正确的方式?

  $ prodcutDetails = ProductA-> with('product_b') - > with('features' ) - >在( '选项') - >找到($ ID); 

谢谢

解决方案

您可以使用点符号来加速关系的关系。像这样:

  $ prodcutDetails = ProductA :: with(['product_b','product_b.features','product_b.features 。选项']) - >找到($ ID); 


I have the following models:

ProductA - ProductB - Feature - Option

I have the following relations:

ProductA belongsToMany ProductB ProductB belongsToMany Feature Feature belongsToMany Option

When I want to see details of ProductA, (a post with ProductA id, that will be managed by a controller) I'd like to eager load all the relationships to pass a variable containing all the details to the view.

Is it possible with a single Eloquent instruction?

Something like this (I know it's not working): is it the correct way?

$prodcutDetails = ProductA->with('product_b')->with('features')->with('option')->find($id);

Thanks

解决方案

You can use the dot notation to eager load relations of relations. Like so:

$prodcutDetails = ProductA::with(['product_b', 'product_b.features', 'product_b.features.option'])->find($id);

这篇关于如何在多层次上加快关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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