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

JoaoHamerski's avatar

Relationship between "Users" and "Products" with pivot "Order" table?

I have 3 tables, users, products and orders, the users and products has a relationship of many-to-many, but, what is the relationship in the methods of the user model? I understand that a product belongs-to-many users but, a user belongs to many products? Is it right? I don't think so.

Or i can do a user hasMany products? Is it will be wrong if i do a user hasMany products in a relationship of many-to-many between users and products? Or the two relations have to be obligatorily a "belongsToMany"?

0 likes
3 replies
tykus's avatar

The method name is belongsToMany on both sides of the relationship whenever you use a pivot table.

JoaoHamerski's avatar

But even if it is a little weird to say that a user belongs to many products?

tykus's avatar

That's the name of the method; I wouldn't stress about it. Your code will be readable as $user->products and $product->users - the relationship method is transparent.

Please or to participate in this conversation.