laravel按天、按小時,查詢數(shù)據(jù)的實例
使用laravel做后臺數(shù)據(jù)統(tǒng)計的時候,需要查詢每天的注冊量之類的數(shù)據(jù)
這時候如果直接用created_at分組,是不好用的。
1、所以本文解決這個查詢應(yīng)該怎么寫。
2、并且推薦一個時間選擇插件,因為統(tǒng)計中一定會用到,本周數(shù)據(jù)、本月、本季度、上個月。。。。
按天分組數(shù)據(jù):
Event::where('created_at','>',Carbon::parse($request->start_date))
->where('created_at','<',Carbon::parse($request->end_date))
//兩個where限制開始結(jié)束時間
->groupBy('date')
->get([DB::raw('DATE(created_at) as date'),DB::raw('COUNT(*) as value')])
->toArray();
如果想按小時分組所有查詢出來的數(shù)據(jù):
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 () {
/*設(shè)置開始結(jié)束時間*/
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,
/*本地化數(shù)據(jù)*/
locale: {
"format": "YYYY/MM/DD",
"separator": " - ",
"applyLabel": "應(yīng)用",
"cancelLabel": "關(guān)閉",
"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')]
}
}, cb);
cb(start, end);
});
超級好用,結(jié)合echart
在用echart的map時候,因為地圖權(quán)限沒有,所以要加載百度地圖。這個坑另開帖子記錄吧。
以上這篇laravel按天、按小時,查詢數(shù)據(jù)的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
php實現(xiàn)屏蔽掉黑帽SEO的搜索關(guān)鍵字
這篇文章主要介紹了php實現(xiàn)屏蔽掉黑帽SEO的搜索關(guān)鍵字的相關(guān)資料,這里推薦給大家,有需要的小伙伴可以參考下。2015-04-04
thinkphp 抓取網(wǎng)站的內(nèi)容并且保存到本地的實例詳解
這篇文章主要介紹了thinkphp 抓取網(wǎng)站的內(nèi)容并且保存到本地的實例詳解的相關(guān)資料,需要的朋友可以參考下2017-08-08
Yii2框架視圖(View)操作及Layout的使用方法分析
這篇文章主要介紹了Yii2框架視圖(View)操作及Layout的使用方法,結(jié)合具體實例形式分析了Yii2框架視圖操作及布局layout相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
WordPress免插件實現(xiàn)面包屑導(dǎo)航的示例代碼
這篇文章主要介紹了WordPress免插件實現(xiàn)面包屑導(dǎo)航,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
php之XML轉(zhuǎn)數(shù)組函數(shù)的詳解
本篇文章是對php中的XML轉(zhuǎn)數(shù)組函數(shù)進行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

