php - With & WhereHas 返回空的嵌套关系
问题描述
我有以下 Eloquent 查询,据我了解,它应该返回与属性集有关系的类别,而属性集又与属性有关系,而属性集又与变体有关系。
$attributes = Category::with(['attributeSets.attributes.variants' => function ($query) {
$query->where('id', 1)->withPivot('value');
}])->whereHas('attributeSets.attributes.variants', function ($query) {
$query->where('id', 1);
})->get()->toArray();
但是,我收到以下似乎包含与变体无关的属性(例如)"variants" => []。
如何删除这些属性?
array:9 [▼
0 => array:12 [▼
"id" => 7
"parent_id" => null
"category_type_id" => null
"name" => "harum"
"identifier" => "harum"
"slug" => "harum"
"sort_order" => 0
"is_active" => 0
"created_at" => "2019-12-11 15:45:51"
"updated_at" => "2019-12-11 15:45:51"
"parent" => null
"attribute_sets" => array:1 [▼
0 => array:8 [▼
"id" => 1
"name" => "ex"
"identifier" => "ex"
"created_at" => "2019-12-11 15:45:51"
"updated_at" => "2019-12-11 15:45:51"
"mainCategory" => array:12 [▶]
"pivot" => array:2 [▶]
"attributes" => array:3 [▼
0 => array:8 [▼
"id" => 1
"attribute_set_id" => 1
"name" => "minus"
"identifier" => "minus"
"created_at" => "2019-12-11 15:45:51"
"updated_at" => "2019-12-11 15:45:51"
"set" => array:6 [▶]
"variants" => []
]
1 => array:8 [▼
"id" => 2
"attribute_set_id" => 1
"name" => "ducimus"
"identifier" => "ducimus"
"created_at" => "2019-12-11 15:45:51"
"updated_at" => "2019-12-11 15:45:51"
"set" => array:6 [▶]
"variants" => []
]
2 => array:8 [▼
"id" => 3
"attribute_set_id" => 1
"name" => "mollitia"
"identifier" => "mollitia"
"created_at" => "2019-12-11 15:45:51"
"updated_at" => "2019-12-11 15:45:51"
"set" => array:6 [▶]
"variants" => array:1 [▶]
]
]
]
]
]
我的模型:
类别
public function attributeSets()
{
return $this->belongsToMany(AttributeSet::class, 'attribute_set_categories');
}
属性集
public function attributes()
{
return $this->hasMany(Attribute::class);
}
属性
public function variants()
{
return $this->belongsToMany(Variant::class, 'variant_attributes');
}