Home > Net >  How I can get total sum of daily values of a column in Laravel?
How I can get total sum of daily values of a column in Laravel?

Time:02-16

There is an orders table with a total column that has more than 1000 records. How I can get the daily sum of total for each day. I mean for Monday, Thursday, Wednesday, .... there are many and different rows, and I want to show the sum of total of each day separately? in UI as:

saturday: 111211,
sunday: 211444,
Monday: 120012000,
Thursday: 1225121,

CodePudding user response:

You might be looking for the function DAYOFWEEK.

DB::table('orders')
    ->selectRaw('DAYOFWEEK(created_at) as weekday, SUM(total) as sales')
    ->groupBy('weekday')
    ->get();

(code's untested)

CodePudding user response:

MySQL Query: SELECT DAYNAME(created_at) as day, SUM(total) as total FROM orders group by day

Laravel Query Builder:

DB::table('orders')
       ->selectRaw('DAYNAME(created_at) as day, SUM(total) as total')
       ->groupBy('day')
       ->get();
  • Related