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

Vashen's avatar

3 Way pivot table help

Tables: Project : id; name;

User : id; name;

Role : id; name;

Pivot_table: project_role_user

-A project has many many roles. -A user has many roles -A user belongs to many projects -A role belongs to many users and projects

how do i write to the pivot table

0 likes
4 replies
Continuum's avatar

Lol. I think you and I are currently writing the same software - I currently have your issue, and the exact same table names.

As far as I know, there is no easy solution to this. Laravel does not support three way pivots - but there is this Laravel 4 library that might help: https://github.com/jarektkaczyk/Eloquent-triple-pivot

I'm extremely inexperienced, so take what I say with a grain of salt. I'm currently considering creating joining tables that contain three id's in them, and then writing my own logic on the model to make it work for my requirements.

1 like
Vashen's avatar

@mstnorris i have no other attributes to my pivot table.

pivot table: project_role_user(project_id, role_id,user_id)

using tinker how would i write to this table?

ive tried $project ->role()->attach(1); this wouldnt work as it would try add to project_role table

erikbelusic's avatar

hey, im encountering something similar. did anyone ever make any progress on it? im currently stuck between a custom 3way pivot model and table, or giving my many2many relationships in one table a true primary key to tie to another table to have many to many relationships on that as well

Please or to participate in this conversation.