相关文章推荐
自信的热水瓶  ·  sys.sp_cdc_change_job ...·  5 月前    · 
飘逸的灯泡  ·  有哪些比较好用的低代码开发平台? - 知乎·  1 年前    · 
性感的小蝌蚪  ·  教你用Wii手柄+蓝牙玩转Android模拟 ...·  1 年前    · 
路过的毛衣  ·  混合现实技术在口腔颌面部肿瘤手术中的应用 ...·  1 年前    · 
耍酷的骆驼  ·  使用Node ...·  1 年前    · 
Code  ›  laravel按天、按小时,查询数据的实例开发者社区
https://cloud.tencent.com/developer/article/1723356
谈吐大方的鸵鸟
1 年前
砸漏

laravel按天、按小时,查询数据的实例

腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
砸漏
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > laravel按天、按小时,查询数据的实例

laravel按天、按小时,查询数据的实例

作者头像
砸漏
发布 于 2020-10-20 14:19:15
2.7K 0
发布 于 2020-10-20 14:19:15
举报
文章被收录于专栏: 恩蓝脚本 恩蓝脚本

使用laravel做后台数据统计的时候,需要查询每天的注册量之类的数据

这时候如果直接用created_at分组,是不好用的。

1、所以本文解决这个查询应该怎么写。

2、并且推荐一个时间选择插件,因为统计中一定会用到,本周数据、本月、本季度、上个月。。。。

按天分组数据:

Event::where('created_at',' ',Carbon::parse($request- start_date))
- where('created_at','<',Carbon::parse($request- end_date))
//两个where限制开始结束时间
- groupBy('date')
- get([DB::raw('DATE(created_at) as date'),DB::raw('COUNT(*) as value')])
- toArray();

如果想按小时分组所有查询出来的数据:

Event::where('created_at',' ',Carbon::parse('2017-01-01'))
- where('created_at','<',Carbon::parse('2017-11-09'))
- groupBy('day')
- get([
//通过date_format()来格式化created_at字段
 DB::raw('DATE_FORMAT(created_at,\'%H\') as day'),
 DB::raw('COUNT(*) as value')])
- toArray()

分享一个时间选择插件

这是官网地址

我把我改好的代码附上:

$(function () {
/*设置开始结束时间*/
 var start = moment().subtract(30, 'days');
 var end = moment().subtract(-1,'day');
 var datas = {};
/*选择之后,将时间重新赋值input*/
 function cb(start, end) {
  $('#reportrange span').html(start.format('YYYY/MM/DD') + ' - ' + end.format('YYYY/MM/DD'));
 $('#reportrange').daterangepicker({
 startDate: start,
 endDate: end,
 /*本地化数据*/
 locale: {
  "format": "YYYY/MM/DD",
  "separator": " - ",
  "applyLabel": "应用",
  "cancelLabel": "关闭",
  "fromLabel": "From",
  "toLabel": "至",
  "customRangeLabel": "自定义",
  "weekLabel": "W",
  "daysOfWeek": ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"
  "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"
  "firstDay": 1
 ranges: {
  '今天': [moment(), moment().subtract(-1, 'days')],
  '昨天': [moment().subtract(1, 'days'), moment()],
  '前7天': [moment().subtract(7, 'days'), moment()],
  '前30天': [moment().subtract(30, 'days'), moment()],
  '本月': [moment().startOf('month'), moment().endOf('month').subtract(-1,'day')],
  '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month').subtract(-1,'day')],
  '所有': [moment("2017-09-25"), moment().subtract(-1, 'days')]
 
推荐文章
自信的热水瓶  ·  sys.sp_cdc_change_job (Transact-SQL) - SQL Server | Microsoft Learn
5 月前
飘逸的灯泡  ·  有哪些比较好用的低代码开发平台? - 知乎
1 年前
性感的小蝌蚪  ·  教你用Wii手柄+蓝牙玩转Android模拟器游戏-百度经验
1 年前
路过的毛衣  ·  混合现实技术在口腔颌面部肿瘤手术中的应用 - PMC
1 年前
耍酷的骆驼  ·  使用Node Js和ForEach循环在cloudinary中进行多次上传
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号