我开始在laravel中使用chartjs,在处理获取数据以显示在图表中的请求时我感到困惑
我有user
表,我想获取一年中每个月创建的用户数
我每个月都有很多用户。但是,它没有描述给出数据的确切月份。如何获得与该月数据相对应的月份?
在我的控制器中,我写了这个代码来查找每月的用户数量。
//count number of created user each month
$viewer = User::select(DB::raw("SUM(status) as count"))
->orderBy("created_at")
->groupBy(DB::raw("month(created_at)"))
->get()->toArray();
$viewer = array_column($viewer,'count');
$countUser = json_encode($viewer,JSON_NUMERIC_CHECK);
// I only have data in Oct and Nov because I created this table on that time
和我在视图刀片中的javascript代码
<script>
var lineChart = document.getElementById('lineChart').getcontext('2d')
var myLineChart = new Chart(lineChart,{
type: 'line',data: {
labels: //here is where show months,datasets: [{
label: "the number of registered users",borderColor: "#1d7af3",pointBorderColor: "#FFF",pointBackgroundColor: "#1d7af3",pointBorderWidth: 2,pointHoverRadius: 4,pointHoverBorderWidth: 1,pointRadius: 4,backgroundColor: 'transparent',fill: true,borderWidth: 2,data: {{$countUser}}
}]
},options : {
responsive: true,maintainAspectRatio: false,legend: {
position: 'bottom',labels : {
padding: 10,fontColor: '#1d7af3',}
},tooltips: {
bodySpacing: 4,mode:"nearest",intersect: 0,position:"nearest",xPadding:10,yPadding:10,caretPadding:10
},layout:{
padding:{left:15,right:15,top:15,bottom:15}
}
}
});
</script>
如果我这样编写代码,尽管数据从10月开始,但数据将从1月开始排序。如何使用QueryBuilder或Eloquent ORM更改我的sql语句来解决它?
感谢您的帮助!