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

probots's avatar

Stuck with Many to Many across multiple Tables. Please help someone ;)

Hi Guys,

i am stuck with a normally simple relationship (with normal queries), but i am stuck with eloquent

Let's say i have 3 tables: account user teams

with 3 pivot tables account_user account_team user_team

So basically every user can have multiple accounts and multiple teams within an account.

And there is my problem, how do i get f.e. the users teams based on one account?

Hope somebody can help me?

Greets Marcus

0 likes
4 replies
probots's avatar

i already looked ionto this, but i thought this isn't the right way to do it?

So do i define one relationship via belongsToMany() to get all teams and one hasManyThrough() to get teams based on one account? How does the query look like when i want the users teams based on account_id f.e. 1?

I am still kinda stuck, sorry ;)

Thanks for your help!

Snapey's avatar

you have the account id but not the account object?

why is it not account->teams ( a teams relationship on the account model)

sorry if i'm being dense

probots's avatar

Yes, i have the account_id and the user_id and i want to get the teams from the user within this account. the thing is that users can be part of an account but not of every team of this account.

cant get my head around this ;)

Please or to participate in this conversation.