热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

Laravel统计一段时间间隔的数据方法

今天小编就为大家分享一篇Laravel统计一段时间间隔的数据方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

获取七天以前到现在的数据:

$days = Input::get('days', 7);

$range = \Carbon\Carbon::now()->subDays($days);

$stats = User::where('created_at', '>=', $range)
 ->groupBy('date')
 ->orderBy('date', 'DESC')
 ->get([
  DB::raw('Date(created_at) as date'),
  DB::raw('COUNT(*) as value')
 ]);

SELECT 
sum(case when `EmailSource`='FM' then 1 else 0 end) as FM_Statistic,
sum(case when `EmailSource`='UOC' then 1 else 0 end) as UOC_Statistic,
sum(case when `EmailSource`='OC' then 1 else 0 end) as OC_Statistic,
DATE_FORMAT(Date,'%Y-%m-%d') AS `DateTime` 
FROM `user_performance` 
WHERE Email != '' AND Email != 'TOTAL'
AND (DATE_FORMAT(Date,'%Y-%m-%d') >= DATE_FORMAT('2011-02-5','%Y-%m-%d')) 
AND (DATE_FORMAT(Date,'%Y-%m-%d') <= DATE_FORMAT('2011-03-07','%Y-%m-%d')) 
GROUP BY `Date`
 public function getNumber()
 {
  $data = [];
  $customers = Customer::all(['id', 'customer_type', 'created_at']);

  #今天数据
  $data['customer_today'] = Customer::where('customer_type', 1)->where('created_at', Carbon::today())->count();
  $data['teacher_today'] = Customer::where('customer_type', 2)->where('created_at', Carbon::today())->count();

  #昨天数据
  $data['customer_yesterday'] = Customer::where('customer_type', 1)->where('created_at', Carbon::yesterday())->count();
  $data['teacher_yesterday'] = Customer::where('customer_type', 2)->where('created_at', Carbon::yesterday())->count();

  $data['today'] = $data['customer_today'] + $data['teacher_today'];
  $data['yesterday'] = $data['customer_yesterday'] + $data['teacher_yesterday'];

  // 本周数据
  $this_week = [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()];
  $data['customer_this_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $this_week)->count();

  $data['teacher_this_week'] = Customer::where('customer_type', 2)->whereBetween('created_at', $this_week)->count();

  // 上周数据
  $last_week = [Carbon::now()->startOfWeek()->subWeek(), Carbon::now()->endOfWeek()->subWeek()];
  $data['customer_last_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $last_week)->count();

  $data['teacher_last_week'] = Customer::where('customer_type', 2)->whereBetween('created_at', $last_week)->count();

  $data['this_week'] = $data['customer_this_week'] + $data['teacher_this_week'];
  $data['last_week'] = $data['customer_last_week'] + $data['teacher_last_week'];

  // 本月数据
  $data['customer_this_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->month)->count();
  $data['teacher_this_month'] = Customer::where('customer_type', 2)->whereMonth('created_at', Carbon::now()->month)->count();

  // 上月数据
  $data['customer_last_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count();
  $data['teacher_last_month'] = Customer::where('customer_type', 2)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count();

  $data['this_month'] = $data['customer_this_month'] + $data['teacher_this_month'];
  $data['last_month'] = $data['customer_last_month'] + $data['teacher_last_month'];

  // 本年数据
  $data['customer_this_year'] = Customer::where('customer_type', 1)->whereYear('created_at', Carbon::now()->year)->count();
  $data['teacher_this_year'] = Customer::where('customer_type', 2)->whereYear('created_at', Carbon::now()->year)->count();

  $data['today_login_users'] = LoginLog::whereDate('created_at', '=', Carbon::today())
   ->groupBy('customer_id')
   ->orderBy('customer_id')
   ->count();

  $data['yesterday_login_users'] = LoginLog::whereDate('created_at', '=', Carbon::yesterday())
   ->groupBy('customer_id')
   ->orderBy('customer_id')
   ->count();

  $data['this_month_login_users'] = LoginLog::whereMonth('created_at', Carbon::now()->month)
   ->groupBy('customer_id')
   ->orderBy('customer_id')
   ->count();

  $data['last_month_login_users'] = LoginLog::whereMonth('created_at', Carbon::now()->subMonth()->month)
   ->groupBy('customer_id')
   ->orderBy('customer_id')
   ->count();

  return $data;
 }
 public function numberCount()
 {
  $days = request('days', 7);

  $range = Carbon::today()->subDays($days);

  $day_stats = Customer::where('created_at', '>=', $range)
   ->groupBy('date')
   ->orderBy('date', 'DESC')
   ->get([
    \DB::raw('DATE_FORMAT(created_at,\'%Y-%m-%d\') as date,SUM(CASE WHEN customer_type = 1 THEN 1 ELSE 0 END) AS customer,SUM(CASE WHEN customer_type = 2 THEN 1 ELSE 0 END) AS teacher'),
   ])
   ->toJSON();

  $week_stats = Customer::groupBy('week')
   ->orderBy('week', 'DESC')
   ->get([
    \DB::raw('DATE_FORMAT(created_at,\'%Y W%u\') as week,SUM(CASE WHEN customer_type = 1 THEN 1 ELSE 0 END) AS customer, SUM(CASE WHEN customer_type = 2 THEN 1 ELSE 0 END) AS teacher'),
   ])
   ->toJSON();
  // dd($week_stats);

  // \DB::enableQueryLog();
  $month_stats = Customer::groupBy('month')
   ->orderBy('month', 'DESC')
   ->get([
    \DB::raw('DATE_FORMAT(created_at,\'%Y-%m\') as month,SUM(CASE WHEN customer_type = 1 THEN 1 ELSE 0 END) AS customer,SUM(CASE WHEN customer_type = 2 THEN 1 ELSE 0 END) AS teacher'),
   ])
   ->toJSON();
  // dd(\DB::getQueryLog());
  // dd($week_stats, $month_stats);
  $data = $this->getNumber();
  // dd($day_stats, $week_stats, $month_stats, $data);
  return view('admin.numberCount', compact('day_stats', 'week_stats', 'month_stats', 'data'));
 }

效果图:

以上这篇Laravel统计一段时间间隔的数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


推荐阅读
  • 本文介绍了如何利用npm脚本和concurrently工具,实现本地开发环境中多个监听服务的同时启动,包括HTTP服务、自动刷新、Sass和ES6支持。 ... [详细]
  • 本文探讨了在通过 API 端点调用时,使用猫鼬(Mongoose)的 findOne 方法总是返回 null 的问题,并提供了详细的解决方案和建议。 ... [详细]
  • 本文详细解析了如何使用Python的urllib模块发起POST请求,并通过实例展示如何爬取百度翻译的翻译结果。 ... [详细]
  • 本文探讨了在UC浏览器中调用分享面板后,图片无法正常显示的问题,并提供了详细的解决方法和代码示例。 ... [详细]
  • 本文介绍如何在PostgreSQL数据库中正确插入和处理JSON数据类型,确保数据完整性和避免常见错误。 ... [详细]
  • 作为一名 Ember.js 新手,了解如何在路由和模型中正确加载 JSON 数据是至关重要的。本文将探讨两者之间的差异,并提供实用的建议。 ... [详细]
  • 本文详细介绍了在企业级项目中如何优化 Webpack 配置,特别是在 React 移动端项目中的最佳实践。涵盖资源压缩、代码分割、构建范围缩小、缓存机制以及性能优化等多个方面。 ... [详细]
  • 选择适合生产环境的Docker存储驱动
    本文旨在探讨如何在生产环境中选择合适的Docker存储驱动,并详细介绍不同Linux发行版下的配置方法。通过参考官方文档和兼容性矩阵,提供实用的操作指南。 ... [详细]
  • 本文详细介绍了Ionic框架的使用方法及其与Angular的集成。Ionic框架是一个强大的前端开发工具,适用于构建跨平台的移动应用程序。文章将探讨如何引入必要的CSS和JavaScript文件,并解释bundle.js中包含的核心功能,如路由等。 ... [详细]
  • 自 Node.js 6.3 版本起,调试功能已内置在核心模块中,无需额外安装 node-inspector 等工具。通过简单的命令即可启动调试模式,并利用 Chrome 浏览器进行高效的代码调试。 ... [详细]
  • 探讨了如何解决Ajax请求响应时间过长的问题。本文分析了一个从服务器获取少量数据的Ajax请求,尽管服务器已经对JSON响应进行了缓存,但实际响应时间仍然不稳定。 ... [详细]
  • This pull request introduces the ability to provide comprehensive paragraph configurations directly within the Create Note and Create Paragraph REST endpoints, reducing the need for additional configuration calls. ... [详细]
  • 本文详细介绍了如何在PHP中使用serialize()和unserialize()函数,以及它们在数据传输和存储中的应用。 ... [详细]
  • Netflix利用Druid实现高效实时数据分析
    本文探讨了全球领先的在线娱乐公司Netflix如何通过采用Apache Druid,实现了高效的数据采集、处理和实时分析,从而显著提升了用户体验和业务决策的准确性。文章详细介绍了Netflix在系统架构、数据摄取、管理和查询方面的实践,并展示了Druid在大规模数据处理中的卓越性能。 ... [详细]
  • 本文探讨了Jsonapi-rb与ActiveModelSerializers (AMS)在性能上的差异,并分享了详细的基准测试结果。 ... [详细]
author-avatar
无奈的双子星_403
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有