laravel 查询年月的时间报表常用代码
xuexi 2024-02-05 12:50:41 发表在:PHP 查看数:667

查询当天数据语句为:

cbsy_code_log::whereDate('created_at', date('Y-m-d'))->get();

laravel 查询年月的时间报表常用代码 控制器:


//计算出每月总数和月份查询数据
  $data = Code_log::select([
        DB::raw('count(id) as zongshu'),
        DB::raw('YEAR(created_at) year, MONTH(created_at) month')
    ])
     ->groupby('year','month') 
    ->get()
    ->toArray();
//
    foreach ($data as  $v) {
       $data1[]= $v['year'].'-'.$v['month'];
       $data2[]= $v['zongshu'];
    }
  $shuju['riqi']=json_encode($data1);
  $shuju['zongshu']=json_encode($data2);

视图代码:

<script>
    var visitorsChart = new Chart($('#visitors-chart'), {
        data: {
            labels:  {!! htmlspecialchars_decode($shuju['riqi'] ) !!},  //月份
            datasets: [{
                type: 'line',
                data:  {!! htmlspecialchars_decode($shuju['zongshu'] ) !!},  //数据
                backgroundColor: 'transparent',
                borderColor: '#007bff',
                pointBorderColor: '#007bff',
                pointBackgroundColor: '#007bff',
                fill: false
            },
            {
                type: 'line',
                data: [],
                backgroundColor: 'tansparent',
                borderColor: '#ced4da',
                pointBorderColor: '#ced4da',
                pointBackgroundColor: '#ced4da',
                fill: false
            }]
        },
        options: {
            maintainAspectRatio: false,
            legend: {
                display: false
            },
            scales: {
                yAxes: [{}],
                xAxes: [{}]
            }
        }
    });
最近访问时间:2025-01-17 11:22:22
知识库:318条鸣谢:TAY  备案号:蜀ICP备2024090044号