Laravel按日期时间格式按小时分组 [英] Laravel group by Hour from datetime Format
本文介绍了Laravel按日期时间格式按小时分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在一天中的某个小时对网站上的注册"进行分组?
How can I group "registrations" on my website by the hour of the day?
我已经尝试过了,但是没有用.
I have tried this but it is not working.
$regs = DB::table('registrations')
->select('createddatetime', DB::raw('COUNT(id)'))
->groupBy(DB::raw('DATEPART(hour, createddatetime)'), 'createddatetime')
->get();
我收到一个错误:
无法访问空属性.
Cannot access empty property.
我期望查询结果超过0个.
I'm expecting more than 0 results from the query.
推荐答案
您可以使用GroupBy函数使其正确
You can use GroupBy function to make it proper
$regs = DB::table('registrations')->select('createddatetime', DB::raw('COUNT(id) as count'))->get();
$regs = $regs->groupBy(function($reg){
return date('H',strtotime($reg->createddatetime));
});
echo '<pre>';
print_r($regs);
这篇关于Laravel按日期时间格式按小时分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文