0

I want to select 12 Month and Sum price from table Product in only this year. First I declare variable starOfMonth and endOfMonth

$from = Carbon::now()->startOfYear()->format('Y-m-d');
$to = Carbon::now()->endOfYear()->format('Y-m-d');

then I query like this

$getTotal = Product::select(
                DB::raw('IFNULL(sum(price),0) as sums'),
                DB::raw('YEAR(date) year, MONTH(date) month')
            )->whereBetween('date', [$from, $to])->groupby('year','month')->get()->toArray();

Display output look like this. I already get right data but How can I display 12 month if no data sum will display in 0

array:4 [▼
  0 => array:3 [▼
    "sums" => 90592.0
    "year" => 2018
    "month" => 6
  ]
  1 => array:3 [▼
    "sums" => 95600.0
    "year" => 2018
    "month" => 9
  ]
  2 => array:3 [▼
    "sums" => 12268424.0
    "year" => 2018
    "month" => 10
  ]
  3 => array:3 [▼
    "sums" => 14353968.0
    "year" => 2018
    "month" => 11
  ]
TryHard
  • 299
  • 4
  • 19

0 Answers0