Laravel eloquent query to return sum of month orders

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"}}}]  


Read more here: https://stackoverflow.com/questions/68477335/laravel-eloquent-query-to-return-sum-of-month-orders

Content Attribution

This content was originally published by m33ts4k0z at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: