Closed Stelikas closed 1 year ago
Hi @Stelikas,
['category_product', Product::class], [Brand::class]
Is this a typo or are passing two separate arrays here? It should be ['category_product', Product::class, Brand::class]
.
If that's not the issue, please share the migrations (local and foreign keys) of all tables involved in this relationship.
Hello @staudenmeir thanks for responding.
I changed the code as below:
public function manufacturers() {
return $this->hasManyDeep(Manufacturer::class, ['category_product', Product::class, Brand::class],
[
'category_id',
'brand_id',
'manufacturer_id',
'id'
],
[
'id',
'product_id',
'id',
'id'
]
);
}
As of the tables:
category --id
category_product --category_id --product_Id
product --id --brand_id
brand --id --manufacturer_id
manufacturer --id
Use this relationship:
public function manufacturers() {
return $this->hasManyDeep(
Manufacturer::class,
['category_product', Product::class, Brand::class],
[
'category_id',
'id',
'id',
'id',
],
[
'id',
'product_id',
'brand_id',
'manufacturer_id',
]
);
}
@staudenmeir I actually tried this and it displays a manufacturer two times.
There must be multiple paths from the category through the category_product
table to this manufacturer (possibly with different products and/or brands). The query result is correct, but they are also ways to only get unique manufacturers.
What does your query look like in this case?
@staudenmeir you can add ->distinct(); at the end of your relationship
I honestly tried everything before writing an issue but i couldn't find a solution. In sort:
I'm trying to grab manufacturers from product categories and the only connection is through the product brand
Category
→ many to many →Product
→ has one →Brand
→ has one →Manufacturer
Inside category.php model
What am i doing wrong?