Based on the table below
I want to get all the records on the created_at column grouped by month and sum the cost month for each record.
created_at
grouped by
sum
cost
Hope I can explain it.
You can do this
$monthlyCosts = DB::table('my_table') ->selectRaw("DATE_FORMAT(created_at, '%m-%Y') as month, SUM(cost) as total_cost") ->groupBy('month') ->get();
You can do this