我有一个使用HasManyThrough
的模型关系,但我不能在产品上加载关系,我发现这个包https://github.com/staudenmeir/eloquent-has-many-deep可以帮助转换我的模型关系与HasManyDeep
public function products()
{
return $this->hasManyThrough(ProductVariant::class, MerchantProduct::class, 'merchant_id', 'id', 'id', 'product_variant_id')
->join('products', 'products.id', '=', 'product_variants.product_id')->select('products.*')->distinct();
}
我试着改变这个,但仍然错误的结果....
public function products()
{
return $this->hasManyDeep(
Product::class,
['merchant_products', ProductVariant::class],
['merchant_id', 'product_id', 'id'],
['id', 'product_variant_id', 'id']
);
}
终于找到了:)
public function products()
{
return $this->hasManyDeep(
Product::class,
['merchant_products', ProductVariant::class],
[null, 'id', 'id'],
[null, 'product_variant_id', 'product_id']
)->distinct();
}