mysql - Laravel 4 Eager Loading filtering and selecting only matching results -


i'm trying output filter results matching elements.

i have 2 tables (in real project, 5), let's companies , projects. company may have more 1 project or may not have any.

these relations:

/app/models/company.php

<?php  class company extends eloquent {      public function projects() {         return $this->hasmany('project','companyid');     }      protected $table = 'companies';   } 

/app/models/project.php

<?php  class project extends eloquent {      public function companies() {         return $this->belongsto('company','companyid');     }  } 

what want is, want results of them both matching parameters.

i've tried this:

return company::with(array('projects'=>function($query){     $query->where('id',99); //project's id 99 }))->get(); 

this output json

if change value 99 1 (there result products.id of 1), changes this:

i want second result second json i've posted.

as can see in second json (i'm using this parser check), companies loaded regardless of project, , rows matched have object projects.

there more 'with's , don't know how filter matching elements.

i tried having() inside closure, it's still same:

$query->having('projects.id','=',99); 

is there way filter matching results (without using loop) output include results having matched projects object?

edit: actually, 5 tables filtered.

companies, projects, works, users , user_works

let's say; "companies" have many projects. "projects" have many works "works" have many users, "users" may have more 1 work (pivot table user_works).

all relations set correctly models.

i want global searching these.

something like: "bring me user id 1's works has company id of 5 , project id of 4", none of fields mandatory. these valid searching: "bring me everyone's works on project id of 2", or "bring me id 2's works", or "bring me works starting today", "bring me id 1's works on project 2", "bring me year's works done of company id 1".

thanks in advance

using wherehas() solution on case. filters relation , affects results returned in main query.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -