How to get the record if Count is zero in Laravel

后端 未结 2 1599
走了就别回头了
走了就别回头了 2021-01-06 13:03

Im using Chart.js to show the number of bookings for each month. This is my query. It is working fine. My problem is if there is no booking in a month then it doesnt show an

2条回答
  •  盖世英雄少女心
    2021-01-06 13:16

    You have to set zero when there are no records for counting.

    Change your query something like this:

    $graph = DB::table('bookings')
    ->select(DB::raw('MONTHNAME(created_at) as month'), 
             DB::raw("DATE_FORMAT(created_At,'%M') as monthNum"),    
             DB::raw('ifnull(count(*),0) as totalbook'))
    ->groupBy('monthNum')
    ->orderBy('created_at', 'asc')
    ->get();
    

    Hope you understand.

提交回复
热议问题