Php Laravel 5.1将关系数据加载到选定行中

Php Laravel 5.1将关系数据加载到选定行中,php,laravel,laravel-5,Php,Laravel,Laravel 5,我的数据库中有一个时间表表和一个用户表。以下关系设置在时间表模型上 /** * The user that owns the timesheet. * * @return Object */ public function user() { return $this->belongsTo('App\Models\User\User'); } 上述情况意味着,当我从数据库中选择时间表时,我可以通过以下方式获取用户数据: $this->timesheet->whe

我的数据库中有一个
时间表
表和一个
用户
表。以下关系设置在
时间表
模型上

/**
 * The user that owns the timesheet.
 *
 * @return Object
 */
public function user()
{
    return $this->belongsTo('App\Models\User\User');
}
上述情况意味着,当我从数据库中选择时间表时,我可以通过以下方式获取用户数据:

$this->timesheet->whereStatus('Approved')->with('user');
这将在结果中加载用户对象,如果转换为数组,则看起来是这样

0 => array:13 [▼
    "id" => 1
    "user_id" => 2
    "week" => 1
    "year" => 2016
    "week_ending" => "Sunday 10th January 2016"
    "total_hours" => "45.00"
    "token" => "0e6796a2dc68066c8d36ff828c519af00657db02b733309b8a4ac0f7b5d6a385"
    "status" => "Approved"
    "supervisor_id" => 1
    "approved_by" => 1
    "created_at" => "2016-01-13 15:42:49"
    "updated_at" => "2016-01-14 14:52:07"
    "user" => array:7 [▼
      "id" => 2
      "first_name" => "Bill"
      "last_name" => "Andrews"
      "email" => "Bill.andrews@domain.com"
      "status" => 1
      "created_at" => "2016-01-13 15:38:18"
      "updated_at" => "2016-01-14 14:50:03"
    ]
  ]
但是,我只需要用户表中的
first\u name
last\u name
。是否有方法将
用户
数组与
时间表
合并/展平,使其看起来像这样

0 => array:14 [▼
    "id" => 1
    "user_id" => 2
    "week" => 1
    "year" => 2016
    "week_ending" => "Sunday 10th January 2016"
    "total_hours" => "45.00"
    "token" => "0e6796a2dc68066c8d36ff828c519af00657db02b733309b8a4ac0f7b5d6a385"
    "status" => "Approved"
    "supervisor_id" => 1
    "approved_by" => 1
    "created_at" => "2016-01-13 15:42:49"
    "updated_at" => "2016-01-14 14:52:07"
    "first_name" => "Bill"
    "last_name" => "Andrews"
  ]
我试着像这样使用急切的加载

$this->timesheet->with(['user' => function ($query) {
    $query->select('first_name', 'last_name');
}])->get()->toArray();
但是,它会产生以下输出:

array:126 [▼
  0 => array:13 [▼
    "id" => 1
    "user_id" => 2
    "week" => 1
    "year" => 2016
    "week_ending" => "Sunday 10th January 2016"
    "total_hours" => "45.00"
    "token" => "0e6796a2dc68066c8d36ff828c519af00657db02b733309b8a4ac0f7b5d6a385"
    "status" => "Approved"
    "supervisor_id" => 1
    "approved_by" => 1
    "created_at" => "2016-01-13 15:42:49"
    "updated_at" => "2016-01-14 14:52:07"
    "user" => null
  ]

你试过要一份清单吗

->lists('first_name', 'last_name');
或者如果您想执行选择

->select('first_name', 'last_name')->get()
更新 您还可以执行即时加载,以即时加载相关对象。这里有一个例子

$users = App\User::with(['posts' => function ($query) {
    $query->select('first_name', 'last_name');

}])->get();

如果有帮助,请告诉我

在这种情况下,最好使用原始SQL或查询生成器。
因为关系用于将数据库数据用作关系对象

在第二个示例中,用户关系为空的原因是,为了使雄辩的关系发挥作用,它需要连接关系的键。换句话说

  • 获取
    时间表
  • 仅使用
    名和
    姓获取
    用户
  • 建立关系
  • 由于未获取用户id,
    用户id
    时间表的用户id
    不匹配,因此无法建立关系
  • 为了使查询正常工作,您需要对其进行如下调整:

    $this->timesheet->with(['user' => function ($query) {
        $query->select('id', 'first_name', 'last_name');
    }])->get()->toArray();
    
    这样,如果您想要一个平坦的结果,我认为最好使用
    连接
    而不是急于加载,因为急于加载的本质

    $this->timesheet
         ->join('users', 'timesheets.user_id', '=', 'users.id')
         ->select('timesheets.*', 'users.first_name', 'users.last_name')
         ->get()->toArray();
    

    这假设您的表名是
    用户
    时间表

    这正是
    联接所做的

    查询生成器还可用于编写连接语句。到 执行基本的SQL“内部联接”,您可以在 查询生成器实例。传递给join方法的第一个参数 是需要连接到的表的名称,而剩余的 参数指定联接的列约束

    如果你想在你的领域更具选择性,你可以选择你所选择的

    $this->timesheet
          ->leftjoin('user', 'user.id', '=','timesheet.user_id')
          ->select('timesheet.*', 'user.first_name', 'user.last_name')
          ->get()
          ->toArray();
    
    正如其他人所建议的,最好使用DB查询生成器,如

    this->timesheet = DB::table('timesheet')
                     ->where('timesheet.status', 'Approved')
                     ->leftjoin('user', 'user.id', '=','timesheet.user_id')
                     ->select('timesheet.*', 'user.first_name', 'user.last_name')
                     ->get()
                     ->toArray();
    

    Laravel模型可以在获取/设置属性之前修改数据。实际上,您可以通过定义getter函数将属性添加到模型中。这将允许您以与
    用户id
    状态
    相同的方式引用用户名。这些函数对于更改视图的日期格式或清理表单输入也很有用

    /**
     * Get the first name of the user.
     *
     * @return string
     */
    public function getFirstNameAttribute()
    {
        return $this->user->first_name;
    }
    
    /**
     * Get the last name of the user.
     *
     * @return string
     */
    public function getLastNameAttribute()
    {
        return $this->user->last_name;
    }
    

    如果你认为我已经正确回答了你的问题,请你为下一个人把这个标记为正确答案。谢谢我尝试了
    $rows->list('week_ending'、'user.first_name'、'user.last_name'、'created_at'、'total_hours'、'status')
    这是我需要的列的列表,但它只返回
    “Bill”=>“2016年1月10日星期日”
    请查看上面的输出(我已经更新了我的原始帖子)。在你粘贴在问题中的渴望加载代码中,我没有看到任何满足此->whereStatus('Approved')的内容你在原件上的声明。是否预期?不需要
    ->whereStatus('Approved')
    。不管怎样,每个时间表都属于一个用户,因此应该显示用户信息。感谢您的帮助,这是我想要的信息。我将在9小时内颁发赏金(它告诉我在那之前我不能颁发)。
    /**
     * Get the first name of the user.
     *
     * @return string
     */
    public function getFirstNameAttribute()
    {
        return $this->user->first_name;
    }
    
    /**
     * Get the last name of the user.
     *
     * @return string
     */
    public function getLastNameAttribute()
    {
        return $this->user->last_name;
    }