0

I am taking sum of all orders by week, month and year. My code is like this:

$current_day = Order::whereDate('created_at', Carbon::today())
                    ->where('order_status', 'Complete')
                    ->sum('total_amount');
$current_week = Order::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
                    ->where('order_status', 'Complete')
                    ->sum('total_amount');
$current_month = Order::whereMonth('created_at', date('m'))
                    ->whereYear('created_at', date('Y'))
                    ->where('order_status', 'Complete')
                    ->sum('total_amount');

How can I take all this information by one SQL request?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Azamat
  • 1
  • 1
  • 2
  • Or this? https://stackoverflow.com/questions/39270169/laravel-5-eloquent-how-to-get-raw-sql-that-is-being-executed-with-binded-data – Nico Haase Aug 23 '21 at 09:49
  • Or this? https://stackoverflow.com/questions/20045732/how-can-i-get-the-raw-query-string-from-laravels-query-builder-before-executing – Nico Haase Aug 23 '21 at 09:49
  • you should use `merge` see this post https://stackoverflow.com/questions/44535539/laravel-how-merge-two-query-results-into-a-single-object – paranoid Aug 23 '21 at 12:58

0 Answers0