渴望负载嵌套的倍数关系
问题描述:
我有一些表:渴望负载嵌套的倍数关系
- 记录
- record_relations
- 标识
- 库存
- 价格
Record
具有多态性关系一起涉及Identification
,Inventory
和Price
。 Inventory
与Metric
和Price
有关系,与Currency
有关系。所以......
-
Record
hasMany
RecordRelations
通过relations()
-
RecordRelation
morphTo
Identification
,Inventory
和Price
通过relation()
-
Inventory
belongsTo
Metric
通过metric()
-
Price
belongsTo
Currency
通过currency()
如果我懒加载一切正常,但我需要加载所有关系(我将它们显示在数据表中);尝试了很多之后,我能够加载最深的关系:如果我添加其他的关系我已经清空所有关系
$builder->with([
'relations' => function($query) {
$query->with('relation.metric')->where('relation_type', '=', 'App\\Inventory');
}
]);
但是......
$builder->with([
'relations' => function($query) {
$query->with('relation');
$query->with('relation.metric')->where('relation_type', '=', 'App\\Inventory');
$query->with('relation.currency')->where('relation_type', '=', 'App\\Price');
}
]);
看来我不能,某些原因,“withWhere
”不止一次。有没有办法做到这一点?
答
定义关系如波纹管
$builder->with([
'relations' => function($query) {
$query->addSelect('column_names');
},
'relation.metric' => function($query){
$query->addSelect('column_names')->where('_condition goes here');
},
'relation.currency' => function($query){
$query->addSelect('column_names')->where('_condition goes here');
}
]);
将column_names替换为要检索的列的名称。 和_condition与您想要应用的条件。
为了访问'关系'我需要通过'relations.relation',这样工作吗? – fred00
没有它没有必要...但如果关系表是从关系表中获取引用,那么它是必需的。 –