问题描述
我有以下型号:
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 ProductBProductB belongsToMany FeatureFeature 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 relationshipsto 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);
这篇关于如何在多层次上加快关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!