laravel 4 - Querying entities with nested many to many relations -


i have problem since started using laravel. when want have entity doesn't have nested relation, life of me , countless hours, couldn't through eloquent has() method. through fluent , left joins.

lets there 3 models 1 pivot (user_persona) carries ids of related models;

example

| user    |       | persona    |       | category    | -----------       --------------       --------------- | user_id | -m-m- | persona_id | -o-m- | category_id | -----------       --------------       | persona_id  |                                        --------------- 

relationships

user

function personas() {     return $this->belongstomany('persona', 'user_persona', 'user_id', 'persona_id'); } 

persona

function categories() {     return $this->hasmany('category', 'persona_id'); } 

problem

now 1 can user persona category through:

user::has('personas.categories', '>=', 1, 'and', function ($q) use ($cid) {     $q->where('category.category_id', '=', $cid) })->first(); 

but can't other users don't have relation same category through;

user::has('personas.categories', '=', 0, 'and', function ($q) use ($cid) {     $q->where('category.category_id', '=', $cid) })->get(); 

eloquent return users including have other relation personas. users included match excluding relation have relation doesn't match it. filtered related personas match condition.

intended similar query in fluent

all in all, want return same results following query:

select     user.*     user u left join user_persona on u.user_id = up.user_id , up.persona_id in          (select persona_id persona_category category_id = ?)     up.persona_id null; 

current eloquent generated query

select     count(*)     `user`     (         select             count(*)                     `persona`         inner join `user_persona` on `persona`.`persona_id` = `user_persona`.`persona_id`                     `user_persona`.`user_id` = `user`.`user_id`         , (             select                 count(*)                             `persona_category`                             `persona_category`.`persona_id` = `persona`.`persona_id`             , `persona_category`.`category_id` = ?         ) = ?     ) >= ? 

i think not using laravel eloquent orm relationships try in user model

public function personas() {     return $this->belongstomany('persona','user_persona'); } 

in persona model

public function users() {     return $this->belongstomany('user','user_persona'); } public function categories() {     return $this->hasmany('category','user_persona'); } 

in category model

public function users() {     return $this->belongstomany('user','user_persona'); } public function personas() {     return $this->hasone('persona','user_persona'); } 

now can use below

$user->personas->categories   //or $personas->users-categories   //or $personas->categories   //or $category->users   //or $category->personas   //or $category->personas->users   // , many other relationship queries eloquent 

and eager loading can save form n+1 query problem

$user=user::with('persona')->find(1)->get(); 

now return user , relationship details , data or persona..

you can refer here - eloquent relationships , eloquent eager loading


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -