Ho due tavoli Usere Post. Uno Userpuò avere molti postse uno postappartiene a uno solo user.
Nel mio Usermodello ho una hasManyrelazione ...
public function post(){
return $this->hasmany('post');
}
E nel mio postmodello ho una belongsTorelazione ...
public function user(){
return $this->belongsTo('user');
}
Ora voglio unire queste due tabelle usando Eloquent with()ma voglio colonne specifiche dalla seconda tabella. So che posso usare Query Builder ma non voglio.
Quando nel Postmodello scrivo ...
public function getAllPosts() {
return Post::with('user')->get();
}
Esegue le seguenti query ...
select * from `posts`
select * from `users` where `users`.`id` in (<1>, <2>)
Ma quello che voglio è ...
select * from `posts`
select id,username from `users` where `users`.`id` in (<1>, <2>)
Quando uso ...
Post::with('user')->get(array('columns'....));
Restituisce solo la colonna dalla prima tabella. Voglio colonne specifiche usando with()dalla seconda tabella. Come lo posso fare?
$query->select('id','username');, stavo ricevendoTrying to get property of non-object