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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -