Laravel Eloquent - Eager loading -


i have 3 tables: client, user, position. client user many-to-many, client position one-to-many , user position many-to-many.

now, user can part of 2 different clients, , each, user assigned positions. want client, , of users, , position.

when use:

client::whereid($id)        ->with(array(          'users',          'users.positions'        ))        ->firstorfail() 

then, users client, positions user has, of clients part of.

what can do?

example

->say have 2 clients: sony , microsoft, , 2 users john , mike.

->say microsoft has positions: developer , engineer.

->say sony has positions: marketing , finance.

john works @ microsoft developer, while mike works @ microsoft engineer , @ sony finance guy.

if want microsoft's employees , positions, expect john (and position being developer) , mike (and position being finance).

however, code above run gives me john (with developer), , mike (with engineer , finance. pivot table finance says sony).

i found answer. had apply constraints eager loading:

<?php client::with(array(           'users',           'users.positions' => function($query) {               $query->whereclientid($id);           }         ))         ->find($id); 

this worked perfectly!


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -