laravel - Eager-loading only some of the properties -
i have 2 models: question
, answer
. question has-many answers. eager-load question's answers, 1 must write this:
$question->load('answers');
however, of answers' properties loaded way. following code, while illustrating want achieve, not work:
$quesiton->load('answers')->select('id', 'body')
so, how can eager-load questions' answers respective id
, , body
properties?
you should try this:
$question->load(['answers' => function ($query) { $query->select('id', 'body', 'question_id'); }]);
as see might not enough load id
, body
. assuming have answer
model question_id
field, should specify in select
otherwise answers won't assigned questions.
Comments
Post a Comment