Laravel Eloquent Group By Month and Year with Sum Example

This simple laravel group by month and sum tutorial demonstrates of laravel group by month and year and sum example. In this laravel groupby tutorial i wanna show you laravel group by month and year with sum example. We will use laravel eloquent group by month and year example. I will use eloquent with selectRaw query method.

I have used created_at column with timestamp and we wanted to get with group by month and year. However, from this tutorial you will learn how to get get month wise data in laravel. So let's see the example of laravel groupby month name and year.

Example of get month name and year wise data in laravel:

//Every month sell data
Route::get('/sells', function () {
	$invoice = Payment::selectRaw('year(created_at) as year, monthname(created_at) as month, sum(total_amount) as total_sale')
            ->groupBy('year','month')
            ->orderByRaw('min(created_at) desc')
            ->get();
	return $invoice;
});

 

Read also: Laravel Group By Date with Multiple Column Sum Example

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using Laravel Vue and React Js Application.