laravel - Subrelationships not using previous conditions -


i'm using laravel 4. have following models:

class user extends eloquent  {      protected $table = 'users';      public function questions()     {         return $this->hasmany('userquestion', 'user_id', 'user_id');     }  }  class userquestion extends eloquent  {      protected $table = 'user_questions';      public function user()     {         return $this->belongsto('user', 'user_id', 'user_id');     }      public function subquestions()     {         return $this->hasmany('usersubquestion', 'question_id', 'id');     }  }  class usersubquestion extends eloquent  {      protected $table = 'user_subquestions';      public function question()     {         return $this->belongsto('userquestion', 'question_id');     }      public function answers()     {         return $this->hasmany('useranswer', 'subquestion_id', 'id');     }  }  class useranswer extends eloquent  {      protected $table = 'user_answers';      public function subquestion()     {         return $this->belongsto('usersubquestion', 'subquestion_id');     }  } 

i have following query:

$results = user::with(['questions' => function($query) {         $query->where('status', '1');         $query->where('category', 'medicine');     }])     ->with('questions.subquestions', 'questions.subquestions.answers')     ->get(); 

however, where conditions i'm applying questions relationship aren't being applied joined tables (subquestions , answers).

how can make conditions apply them well?

note: values of status , category in conditions dynamic (i.e. won't 1 or medicine).

try doing call instead:

$results = user::with(['questions' => function($query) {         $query->where('status', '1');         $query->where('category', 'medicine');     },     'questions.subquestions',     'questions.subquestions.answers' ])->get(); 

note, time method with called once.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -