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

V4n*_*ll4 11 php laravel laravel-5

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

/**
 * The user that owns the timesheet.
 *
 * @return Object
 */
public function user()
{
    return $this->belongsTo('App\Models\User\User');
}
Run Code Online (Sandbox Code Playgroud)

以上意味着当我通过使用以下内容从数据库中选择时间表时,我可以获取用户数据:

$this->timesheet->whereStatus('Approved')->with('user');
Run Code Online (Sandbox Code Playgroud)

这将在结果中加载用户对象,如果转换为数组将如此;

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"
    ]
  ]
Run Code Online (Sandbox Code Playgroud)

不过,我只需要first_name和last_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"
  ]
Run Code Online (Sandbox Code Playgroud)

我曾试图像这样使用急切的装载;

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

但是,它会产生以下输出;

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
  ]
Run Code Online (Sandbox Code Playgroud)

Tho*_*Kim 7

在第二个示例中,用户关系为空的原因是因为为了使Eloquent关系起作用,它需要绑定关系的键.换一种说法...

  1. 得到timesheet.
  2. 获取user只有first_name和last_name.
  3. 建立关系.
  4. 因为你没有获取用户的ID,在user's id和timesheet's user_id不匹配这样的关系不能建立.

为了使您的查询有效,您需要像下面这样调整它:

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

如果你想要一个平坦的结果,我认为最好使用joins而不是急切的加载,因为渴望加载的性质.

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

这假定您的表名是users和timesheets.