简体   繁体   English

如何使用 laravel 7 在 eloquent 查询中获取每月注册用户总数?

[英]how to get the total number of registered users per month in an eloquent query with laravel 7?

I need help in the following... I want to graph the result of the query... I am using the consoleTVCharts library for it... but I have problems when graphing the results this is the code of my query:我需要以下帮助...我想绘制查询结果...我正在使用 consoleTVCharts 库...但是在绘制结果时遇到问题这是我的查询代码:

$users = User::select(\DB::raw("COUNT(*) as count"))
    ->whereYear('created_at' ,'=', '2020')
    ->groupBy(\DB::raw("Month(created_at)"))
    ->pluck('count');

the code to create the graph创建图表的代码

$chart4 = new RegistroUsuariosMensual;
    $chart4->title('Total User Monthly');
    $chart4->labels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']);
    $chart4->dataset('meses','line',$users);

and this is the result这就是结果在此处输入图像描述

and in my database I have no registered users in those months在我的数据库中,在那几个月里我没有注册用户在此处输入图像描述

what will be the problem?会有什么问题?

You can't user groupBy in a integer number, try this您不能在 integer 号码中使用 groupBy,试试这个

$users = User::select(\DB::raw('strftime("%m", created_at) as month, count(id) as total'))
->whereBetween('created_at', ['2020-01-01 00:00:00', '2020-12-31 23:59:59'])
->groupBy('month')
->orderBy('month')
->pluck('total', 'month')
->all();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM