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

cchacholiades's avatar

Relationships

I am using Laravel 4 and I am bit confused with the way I should write the relationships in my project. I have three tables categories, brands and products. I have declared the following relationships:

Categories: belongs to many products and has many brands

Brands: has many products and belongs to many categories

Products: belongs to many categories and belongs to a brand

Is this the correct way to define their relationships? Is it somewhere appropriate to use a pivot table (such products-categories)?

0 likes
2 replies
ATOM-Group's avatar
Level 11

Your relationships, conceptually, sound fine. You'll just need the correct database structure and relationship calls to accurately represent them. Admittedly, this can be confusing in Eloquent when it's one to one or one to many as it's hard to know which direction to model the relationship.

Your category - brand relationship is a many-to-many, so yes, it will need a pivot table to store that information. Same is true of the products - categories relationship.

Any time you have a many to many relationship, you need a pivot table to store that information.

Please or to participate in this conversation.