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

panthro's avatar

Has Many Through with a Belongs To

A product belongs to a country. A country has many products.

A product belongs to a category. A category has many products.

With the above relationships, I'd assign a category_id and a country_id to a product.

Is there any way to find out, through products , what countries a category has?

For example, a TV product belongs to the country Belguim and the category Electronics. When I look under the category electronics, I would want to get all the countries that have electronics, in this case that would be Belgium.

I've looked at hasManyThrough but there is a belongsTo relationship involved...

category hasMany product belongsTo country

0 likes
9 replies
cwhite's avatar

On the other, it's possible to get the info you want with the hasManyThrough laravel method, you just have to reverse the keys for the BelongsTo relationship

class Category extends Model
{
    public function countries(): HasManyThrough
    {
        return $this->hasManyThrough(
            Country::class,
            Product::class,
            'category_id', // Foreign key on the product table
            'id', // Foreign key on the country table...
            'id', // Local key on the category table...
            'country_id' // Local key on the product table...
        );
    }
}

https://laravel.com/docs/9.x/eloquent-relationships#has-many-through-key-conventions

1 like
panthro's avatar

@cwhite I dont' think the reversing of the keys for the BelongsTo relationship works, the country table has no foreign key.

cwhite's avatar

@panthro The comments come from the laravel docs for HasManyThrough relationships... I just updated the table names. Have you tested it? The county table should have an id column.

panthro's avatar

@cwhite I've had a play but cant figure it out, could you explain it a little more if possible?

cwhite's avatar

@panthro

Sure, what does your code look like?

You should be able to copy the below method into your Category model and have access to countries through the relationship.

class Category extends Model
{
    public function countries(): HasManyThrough
    {
        return $this->hasManyThrough(
            Country::class,
            Product::class,
            'category_id',
            'id',
            'id',
            'country_id'
        );
    }
}
francesanderson's avatar

Certainly! It seems like you're dealing with a complex relationship setup involving "Has Many Through" with a "Belongs To" relationship. In your scenario, you have products assigned with a category_id and a country_id. You're wondering if it's possible to determine, through products, the countries associated with a specific category. To achieve this, you can utilize the "Has Many Through" relationship in Laravel. Although you mentioned the presence of a "Belongs To" relationship, it won't hinder you from using "Has Many Through" as long as you have the necessary foreign keys set up correctly. https://www.pikashow8k.com/

Please or to participate in this conversation.