问题描述
我的 Laravel 8 项目有一个函数和一个从我的数据库中获取数据的查询,我想将我的映射器中返回的 first
项转换为一个数组,以便我可以从中的值覆盖它的值我的 $stats
数组。
我该如何转换才能达到这个结果?
我需要返回包含我的数据的整个对象,而 diskSpacetotal
只是我的字段之一,我不想在这里手动写出每个对象键,因此我为什么要尝试使用第一个项目,然后用我需要的内容覆盖:
/**
* Get average history
*/
public function getAverageHistory($agent,$from)
{
$history = DB::table('data_profiler_agents')
->where('agent',$agent)
->join('data_profiler_agents_history',function ($join) use (&$from) {
$join->on('data_profiler_agents.id','=','data_profiler_agents_history.agent_id')
->where('data_profiler_agents_history.created_at','>=',$from)
->where('data_profiler_agents_history.created_at','<=',Carbon::Now())
->orderBy('created_at','asc');
})
->get();
$history = $history->groupBy(function($reg) {
$date = Carbon::parse($reg->created_at);
return $date->format('Y-m-d H:00:00');
});
$history = $history->map(function ($item,$key) {
$stats = [
'diskSpacetotal' => $item->sum('diskSpacetotal')
];
// this part can't be converted to an Array,what's the workaround
return array_merge($item->first()->toArray(),$stats);
});
return $history;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)