[英]How to count and group by month or years with month at zero
I am trying to set up a small statistical module. 我正在尝试建立一个小型统计模块。 I am looking for an array containing for each month the number of events of my 'evenement' table.
我正在寻找一个包含每个月我的'evenement'表事件数量的数组。 With the query below I get the results but only if there is a result in the month.
通过下面的查询,我得到了结果,但仅在月份中有结果时才会得到结果。 IF there is none, it does not appear.
如果没有,则不会出现。 My request:
我的请求:
$data = DB::table("evenements")
->select(DB::raw('EXTRACT(MONTH FROM datedevenement) AS month, COUNT(id) as id'),
DB::raw('ifnull(count(id),0) as id')
)
->where('typeevenement_id', '1')
->whereYear('datedevenement', Carbon::now()->year)
->orderBy('datedevenement')
->groupBy(DB::raw('month'))
->get();
return $data;
My evenement table : 我的evenement表:
id | datedevenement | typeevenement_id
I understand that my request can not invent months that do not exist. 我知道我的请求不能发明不存在的月份。 I wonder if carbon or laravel does not have something to list by month or year in a continuous way.
我想知道碳或laravel是否无法按月或按年连续列出。
Result must be an array or collection with monthORyears->count(evenement) 结果必须是包含monthORyears-> count(evenement)的数组或集合
The simplest thing could be to do a foreach of months and conflict it with the collection, if not present add it. 最简单的事情可能是做几个月的事情并将其与集合冲突,如果不存在则添加它。
For simplicity you can apply the method keyby() ( documentation ) 为简单起见,您可以应用方法keyby()( 文档 )
->keyBy('month')
on your collection. 在你的收藏。
and then perform the foreach like this: 然后像这样执行foreach:
$months =["January", "February", ....];
foreach ($months as $month){
if (!isset($data[$month])){
$data[$month]['id'] = 0;
}
}
I liked Claudio's answer, but I'd try another way. 我喜欢克劳迪奥的回答,但我会尝试另一种方式。
First, I get a collection of my events: 首先,我收集了一些我的活动:
$temp = $model
->orderBy("date_field")
->get()
->groupBy(function($query) {
return Carbon::parse($query->date_field)
->format("m");
})
Now, you have a collection grouped by month. 现在,您有一个按月分组的集合。
My second step was iterate with the collection $temp
like that: 我的第二步是使用集合
$temp
迭代:
$result = new array();
$temp->each(function($query) use (&$result) {
$result["count"] = $query->count();
$result["data"] = $query;
});
Now, you have an array with a collection to explore your data and a counter to know how much events will be realised per month. 现在,您有一个数组,其中包含用于浏览数据的集合,以及一个计数器,用于了解每月将实现的事件数量。 When you haven't event it's necessary to sanitizer with your frontend.
如果您没有活动,则必须使用您的前端进行消毒。
Hope that help you. 希望对你有所帮助。
Reference: https://laraveldaily.com/laravel-group-query-result-by-day-month-year/ 参考: https : //laraveldaily.com/laravel-group-query-result-by-day-month-year/
Thanks you. 谢谢。 With this 2 answers and a small serach i arrived to this solution (i have add somathing for order with current month for start point) :
有了这两个答案和一个小的搜索我到达这个解决方案(我已添加somathing订单与当前月份的起点):
$monthly_uploaded_product = Evenement::select(DB::raw('COUNT(id) as total, EXTRACT(MONTH FROM datedevenement) AS month')
)
->where('typeevenement_id', '1')
->wheredate('datedevenement', '>=', Carbon::now()->lastOfMonth()->subyears(1))
/// ->whereYear('datedevenement', Carbon::now()->year)
->groupBy(DB::raw('month'))
->get();
$mois = ["janvier", "fevrier", "mars", "avril", "mai", "juin", "juillet", "aout", "septembre", "octobre", "novembre", "decembre"];
foreach ($mois as $mois99) {
$arriveeparmoisrefuge[]= array(
'mois' => $mois99,
'total' => '0'
);
}
foreach ($monthly_uploaded_product as $key) {
$arriveeparmoisrefuge[$key->month - 1]['total'] = $key->total;//update each month with the total value
}
$sorted = collect($arriveeparmoisrefuge)->sortBy(function ($count, $month) {
$currentMonth = (int) \Carbon\Carbon::now()->month;
return ($month + (13 - $currentMonth - 1)) % 12;
});
return $sorted;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.