Laravel: filter and group based on created_at and sum costs
P粉314915922
P粉314915922 2023-09-10 00:05:56
0
1
452

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.

Hope I can explain it.

P粉314915922
P粉314915922

reply all(1)
P粉868586032

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();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template