我在 Eloquent 中有 2 个连接表,即主题和用户。
主题模型:
public function user() {
return $this->belongs_to('User');
}
用户模型:
public function themes() {
return $this->has_many('Theme');
}
我的 Eloquent api 调用如下所示:
return Response::eloquent(Theme::with('user')->get());
返回主题的所有列(很好),以及用户的所有列(不好)。我只需要用户模型中的“用户名”列,我该如何限制查询?
最佳答案
更改模型以指定要选择的列:
public function user() {
return $this->belongs_to('User')->select(array('id', 'username'));
}
别忘了包括您要加入的列。
关于php - Laravel Eloquent : How to get only certain columns from joined tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14727038/