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

jginorio's avatar

Choose between hasMany and belongsToMany relationship

Background Information:

I'm building an event management system for a sound company. It will allow them to better allocate their personnel. The company can be hired to provide sound reinforcement for many events.

An event can have many shifts. A shift is for setting up the equipment, running the show, and taking down the equipment.

Then, a shift is fulfilled by users who can hold many positions in the shift.

The user, on the other hand, can hold many positions.

NOTE: A user is not limited to a single position in the shift.

What I want:

I hardcoded some HTML to get the output I desire. I will attach the pictures below.

shift1 shift2 shift3

Basically, I want the admins of the company to sign up a user into a shift. In order to fulfill the necessary positions.

The Problem:

I think I have the database design correct but I'm still debating where to use a belongsToMany relationship and hasMany relationship.

Here is my initial database design:

Initial database design

0 likes
3 replies
bobbybouwmann's avatar

I would probably go for the many-to-many approach. The reason being is that it offers you more flexibility if you need to collect data for relational data. My personal experience is also that use a many-to-many with planning people for a position or a shift is more flexible. You can more easily add a start date or more specific information.

So let's say you have a default pay for a position. You can use the many-to-many to override that pay field for that specific position. In your case, you already store it in the pivot table. But I guess you understand what I mean ;)

jginorio's avatar

You read my mind! I'm planning on adding the default pay in the positions table but I wanted to solve this problem first. HAHA I feel like that a many to many is more flexible in that way but when trying to save a new record is a pain because of the specific formatting the sync() method needs to receive the data.

bobbybouwmann's avatar

Yeah, that can be challenging. But you can build up an array and pass that to the sync method for that. It requires some extra work, but it gives a lot more flexibility in the end on functional level.

Please or to participate in this conversation.