Laravel統(tǒng)計(jì)一段時(shí)間間隔的數(shù)據(jù)方法
獲取七天以前到現(xiàn)在的數(shù)據(jù):
$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']);
#今天數(shù)據(jù)
$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();
#昨天數(shù)據(jù)
$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'];
// 本周數(shù)據(jù)
$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();
// 上周數(shù)據(jù)
$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'];
// 本月數(shù)據(jù)
$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();
// 上月數(shù)據(jù)
$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'];
// 本年數(shù)據(jù)
$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統(tǒng)計(jì)一段時(shí)間間隔的數(shù)據(jù)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
PDO預(yù)處理語句PDOStatement對象使用總結(jié)
這篇文章主要介紹了PDO預(yù)處理語句PDOStatement對象使用總結(jié),本文介紹了PDOStatement的方法及常用方法的使用例子,需要的朋友可以參考下2014-11-11
mysql alter table命令修改表結(jié)構(gòu)實(shí)例詳解
這篇文章主要介紹了mysql alter table命令修改表結(jié)構(gòu)實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-09-09
Codeigniter實(shí)現(xiàn)智能裁剪圖片的方法
這篇文章主要介紹了Codeigniter實(shí)現(xiàn)智能裁剪圖片的方法,可以做到裁剪后不失真,盡可能保留圖片主題含義。需要的朋友可以參考下2014-06-06
PHP安裝擴(kuò)展mcrypt以及相關(guān)依賴項(xiàng)深入講解
這篇文章主要介紹了PHP安裝擴(kuò)展mcrypt以及相關(guān)依賴項(xiàng)深入講解,步驟講解的很清晰,有需要的同學(xué)可以研究下2021-03-03
PHP 實(shí)現(xiàn)類似js中alert() 提示框
這篇文章主要介紹了PHP 實(shí)現(xiàn)類似js中alert() 提示框功能,非常的實(shí)用,這里推薦給大家,有需要的小伙伴來參考下,希望大家能喜歡。2015-03-03
Yii框架使用PHPExcel導(dǎo)出Excel文件的方法分析【改進(jìn)版】
這篇文章主要介紹了Yii框架使用PHPExcel導(dǎo)出Excel文件的方法,結(jié)合實(shí)例形式分析了Yii框架配置、導(dǎo)入PHPExcel組件以及調(diào)用PHPExcel導(dǎo)出Excel文件的相關(guān)操作技巧,需要的朋友可以參考下2019-07-07

