I have three tables: Client
, User
, Position
. Client to User is many-to-many, Client to Position is one-to-many and User to Position is many-to-many.
Now, a User
can be part of two different Clients
, and so for each, this user is assigned to some positions. I want to get the client, and all of its users, and their position.
When I use:
Client::whereId($id)
->with(array(
'users',
'users.positions'
))
->firstOrFail()
Then, I do get all the users for the client, but I get all the positions the user has, from all of the clients they are part of.
What can I do?
Example
->Say I have two clients: Sony and Microsoft, and two users John and Mike.
->Say that Microsoft has positions: Developer and Engineer.
->Say that Sony has positions: Marketing and Finance.
John works at Microsoft as Developer, while Mike works at Microsoft as an Engineer and at Sony as a Finance guy.
If I want to get all Microsoft's employees and their positions, I expect to get John (and his position being Developer) and Mike (and his position being Finance).
However, the code above that I run gives me John (with Developer), and Mike (with Engineer and Finance. The pivot table for Finance clearly says it is for Sony).