laravel请求耗时(Laravel统计一段时间间隔的数据方法)
类别:编程学习 浏览量:2397
时间:2021-10-13 00:28:59 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统计一段时间间隔的数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
原文链接:https://blog.csdn.net/zhezhebie/article/details/78622534
您可能感兴趣
- laravel数据库日志(Laravel 将数据表的数据导出,并生成seeds种子文件的方法)
- laravel模块教程(Laravel框架创建路由的方法详解)
- laravel数据库切换(实现laravel 插入操作日志到数据库的方法)
- laravel数据库关联查找(laravel 操作数据库常用函数的返回值方法)
- laravel 用户登录认证(Laravel5.5 实现后台管理登录的方法自定义用户表登录)
- laravel事务状态(laravel dingo API返回自定义错误信息的实例)
- laravel框架详解(laravel 5.3 单用户登录简单实现方法)
- laravel前后端分离模块化(laravel 实现划分admin和home 模块分组)
- laravel框架的运行机制(Laravel5框架自定义错误页面配置操作示例)
- echart图表有哪些(使用laravel和ECharts实现折线图效果的例子)
- laravel 数据库实现原理(laravel 数据迁移与 Eloquent ORM的实现方法)
- laravel服务提供者应用场景(Laravel如何创建服务器提供者实例代码)
- laravel如何设置默认模块(laravel-admin 中列表筛选方法)
- laravel接口响应时间(laravel请求参数校验方法)
- laravel关于视图使用语法(在laravel中使用with实现动态添加where条件)
- laravel使用队列(基于laravel where的高级使用方法)
- 重庆旅游攻略(重庆旅游3-4天攻略最佳线路)
- 周杰伦演唱会门票(周杰伦演唱会门票多少钱一张2023)
- 焕然一新 成都轨道集团官方网站改版上线(成都轨道集团官方网站改版上线)
- 成都轨道交通19号线二期全线电通(成都轨道交通19号线二期全线电通)
- 19号线二期全线电通 轨道交通项目最新进展来了(19号线二期全线电通)
- 涉及3条地铁线路 成都这4座轨道交通站点有新名字了(涉及3条地铁线路)
热门推荐
- php连接mysql需要数据源吗(php连接mysql数据库最简单的实现方法)
- mysql乐观锁与mvcc(MySQL中的乐观锁,悲观锁和MVCC全面解析)
- docker节点不能启动(解决docker中ifconfig不可用的问题)
- python本地ocr库(详解Python安装tesserocr遇到的各种问题及解决办法)
- python 数据库实现学生管理系统(python+mysql实现教务管理系统)
- python制作彩色字符(Python3利用print输出带颜色的彩色字体示例代码)
- vue加element ui弹窗(Vue中ElementUI分页组件Pagination的使用方法)
- linux中swap分区的作用是(linux swap交换分区详解)
- phpstudy怎么配置域名并访问(phpstudy怎么绑定目录实现域名访问)
- mysql数据库基本增删改查基本语句(MySQL表的增删改查基础教程)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9