我有两个表:状态和status_logs。Laravel 5/Eloquent - 根据created_on列加可变小时返回日期
的状态表有3列:ID,名,时间
的status_logs表有3列:ID,STATUS_ID,created_at
我有两个看起来像这样的模型:
class Status extends Model{
// Name of our database table
protected $table = 'statuses';
// Defines the relationship between this table and the status_logs table
public function status_logs(){
return $this->hasMany('App\Models\Status', 'status_id');
}
}
class StatusLog extends Model{
// Name of our database table
protected $table = 'status_logs';
// Defines the relationship between this table and the statuses table
public function statuses(){
return $this->belongsTo('App\Models\Status', 'status_id');
}
}
我可以用得到两个表的数据:
StatusLog::with('status')->get();
结果看起来是这样的:
"status_logs": [{
"id": 1,
"status_id": 1,
"created_at": "02:34:53 10/5/2017",
"statuses": {
"id": 1,
"name": "started"
"duration": 48
}
}]
我想添加一列名为finish_at到status_logs数组中的每个对象。此日期时间将是created_at值加上任何整数值持续时间是。 持续时间是我们需要添加到created_at以获得值finish_at的小时数。
结果应该是这样的:
"status_logs": [{
"id": 1,
"status_id": 1,
"created_at": "02:34:53 10/5/2017",
"finish_at": "02:34:53 10/7/2017",
"statuses": {
"id": 1,
"name": "started"
"duration": 48
}
}]
我将如何做到这一点?