vendredi 10 juillet 2020

Is there a way to return 0 for months with no data?

I'm using Chart.js to show the total number of transactions (count) for each month.

$dataTotal = ModelName::select(DB::raw('count(id) as count'), DB::raw("MONTH(created_at)  as month")) 
                        ->where('org_id', auth()->user()->org_id)
                        ->where('result_code', 200)
                        ->groupBy('month')
                        ->orderBy('month')
                        ->get()
                        ->toArray();

The query itself is fine, except for the problem that if there is no records in a month then it doesn't return 0 for that month. Which results in the graph not rendering properly.

Does anyone have any suggestions, on how i can implement it so that i receive full resultsets?

The desired format is:

array (
0 => 
array (
  'count' => 0,
  'month' => 1,
),
1 => 
array (
  'count' => 5,
  'month' => 2,
),
2 => 
array (
  'count' => 0,
  'month' => 3,
),
3 => 
array (
  'count' => 4,
  'month' => 4,
),
4 => 
array (
  'count' => 0,
  'month' => 5,
),
5 => 
array (
  'count' => 4,
  'month' => 6,
),
6 => 
array (
  'count' => 51225,
  'month' => 7,
),
7 => 
array (
  'count' => 4,
  'month' => 8,
),
8 => 
array (
  'count' => 0,
  'month' => 9,
),
9 => 
array (
  'count' => 0,
  'month' => 10,
),
10 => 
array (
  'count' => 0,
  'month' => 11,
),
11 => 
array (
  'count' => 0,
  'month' => 12,
),

)  


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2OfEc3a
via IFTTT

Aucun commentaire:

Enregistrer un commentaire