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

Cristhofer Alencar's avatar

Help defing best table relationship

Hi people! I'm was assigned to this on going project and now I'm trying to put order in the caos that it is this project. That beeing said I need help to define the best relationship for these tables so I can query them in diferrents cenarios.

My tables:

Users: id - int name - text

Roles: id - int title - text

Processos: id - int description - text

Services: id - int description - text

Processo_Service: processo_id - int (foreign key to Processoss table) service_id - int (foreign key to Services table) user_id - int (foreign key to Users table) role_user - int (foreign key to Roles table)

So, Processo has "belongToMany" relationship with Services and vice-vera. Processo may have many Services, Services need to be assigned to a User with a specific Role (Photographer or Editor). So to accomplish this I've tried to add "user_id" and "role_user" to the pivot table of Processos and Services. Now I would like to do some queries like.

"Processo::photographer()" and get all entries in the pivot table that has the "role_user" equals to Photographer even if the "processo_id" is the same for more then one entry. Then I would do the same for Editors.

How can I do that? The relationships that I have right now is really the best option?

P.S: I hope I made myself clear, if not I'll be glad to refactor the question and try to be more clear. If need be I can restructure the DB, I just would like to keep the changes to the DB to a minimum cause it's a very large DB and I don't have a lot of time to spend in this issue(I've already spent half a day trying to figure this out :( ).

P.S.2: So far I've managed to get the entries from pivot table filter out by role_user, but the results are grouped by processo_id, even though I'm not using groupBy().

0 likes
0 replies

Please or to participate in this conversation.