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
Post a Comment