php - Laravel Eloquent : How to get only certain columns from joined tables

标签 php laravel eloquent

我在 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/

相关文章:

php - laravel Eloquent 搜索查询

php - 将变量传递给 mysql 语句

PHP 输入文件错误,用 Post 方法回显文件名

laravel - Docker Web 和 api 拒绝连接

php - laravel eloquent where like 整数运算符

php - Laravel 插入嵌套 Eloquent ORM 模型

php - 当图像中的 onclick 事件插入到数据库中

PHP 在特定数量的递归循环后停止执行

php - 如何在 Laravel 5.2 中编写 >= Eloquent 数据库查询

php - 将一个表与其他 4 个表连接起来 laravel eloquent