Laravel eloquent query to return sum of month orders

  eloquent, laravel

I have so far came up with this query:

Order::select(DB::raw('sum(price) as sums'), 
              DB::raw("DATE_FORMAT(created_at,'%e') as day")                                             
             )
             ->where('seller_id', Auth::user()->id)
             ->whereBetween('created_at', [Carbon::now()->startOfMonth()
             ->subMonth(),Carbon::now()->endOfMonth()->subMonth()])
             ->orderBy('created_at', 'asc')
             ->groupBy('day')
             ->get();

This returns an array like this:

[{"sums":145867,"day":"3"},{"sums":19567,"day":"28"}]  

This is almost what I need. I would now like to include even the name of the month in these results, like this:

[{"June" : {{"sums":145867,"day":"3"},{"sums":19567,"day":"28"}}}]  

Source: Laravel

Leave a Reply