我有两张桌子,User
and Post
. One User
可以有很多posts
和一个post
只属于一个user
.
In my User
型号 我有一个hasMany
关系...
public function post(){
return $this->hasmany('post');
}
而在我的post
型号 我有一个belongsTo
关系...
public function user(){
return $this->belongsTo('user');
}
现在我想使用连接这两个表Eloquent with()
但想要第二个表中的特定列。我知道我可以使用查询生成器,但我不想这样做。
当在Post
我写的模型...
public function getAllPosts() {
return Post::with('user')->get();
}
它运行以下查询...
select * from `posts`
select * from `users` where `users`.`id` in (<1>, <2>)
但我想要的是...
select * from `posts`
select id,username from `users` where `users`.`id` in (<1>, <2>)
当我使用...
Post::with('user')->get(array('columns'....));
它仅返回第一个表中的列。我想要使用特定的列with()
从第二个表。我怎样才能做到这一点?