Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

crazydan's avatar

hasManyThrough issue

Hi,

I have the next 3 tables

---Products
-id
-name
-category
-manufacturer

---Categories
-id
-name

---Manufacturers
-id
-name

In Category Model i want to make a relation to get all the manufacturers that products have in that category

i try with

return $this->hasManyThrough('App\Models\Manufacturer', 'App\Models\Product', 'category');

no success the he returns me wrong query

select `manufacturers`.*, `products`.`category` from `manufacturers` inner join `products` on `products`.`id` = `manufacturers`.`product_id` where `products`.`category` = 172

I need the next query:

select `manufacturers`.*, `products`.`category` from `manufacturers` inner join `products` on `products`.`manufacturer` = `manufacturers`.`id` where `products`.`category` = 172

Some idea?

Thank you

0 likes
3 replies
TerrePorter's avatar

What about if you specify both column keys

return $this->hasManyThrough('App\Models\Manufacturer', 'App\Models\Product',  column_from_product_table, column_from_manufacturer_table );
JarekTkaczyk's avatar
Level 53

@crazydan This is not hasManyThrough. You can use belongsToMany with products being pivot table.

1 like
crazydan's avatar
public function _manufacturers()
{
    return $this->belongsToMany('App\Models\Manufacturer', 'products', 'category', 'manufacturer');
}

works perfect :D thank you JarekTkaczyk

Have a good day

Please or to participate in this conversation.