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


Laravel 5.1 loading relationship data into selected row

我的数据库中有一个timesheet表和一个user表。 在timesheet模型上设置以下关系。

/**
 * 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_namelast_name。 有没有办法将user数组与timesheet合并/展平,使其看起来像这样;

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
  ]

第二个示例中,用户关系之所以为 null,是因为为了使 Eloquent 关系正常工作,它需要绑定关系的键。换句话说...

  1. 得到timesheet .
  2. 只用first_namelast_name user
  3. 建立关系。
  4. 由于您没有获取用户的 id,因此user's idtimesheet's user_id不匹配,因此无法建立关系。

为了使查询正常工作,您需要像这样调整它:

$this->timesheet->with(['user' => function ($query) {
    $query->select('id', 'first_name', 'last_name');
}])->get()->toArray();

有了这个,如果你想要一个扁平化的结果,我认为最好使用joins而不是急切加载,因为急切加载的性质。

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

这假定您的表名是userstimesheets

您是否尝试过获取列表

->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();

如果有帮助,请告诉我。

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

/**
 * 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;
}
这正是

join所做的。

从文档

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

$this->timesheet
      ->leftjoin('user', 'user.id', '=','timesheet.user_id')
      ->get()
      ->toArray();

如果你想在你的领域更有选择性,你可以选择你select;

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

正如其他人所建议的那样,最好使用数据库查询构建器,例如

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();

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