Laravel Eloquent Group By Year with Sum Example
This simple article demonstrates of laravel group by year and sum example. i would like to show you laravel group by year with count. We will use laravel eloquent group by year example. you will learn group by year in laravel eloquent example.
Sometimes, we have created_at column with timestamp and we wanted to get with group by with year, that way we can use for chart. However, you can do it using mysql DATE_FORMAT() function. we can use DB raw with mysql function and get group by year records. you can easily use this example with laravel 6, laravel 7, laravel 8 and laravel 9 version.
Let's see bellow example:
Laravel Eloquent Query:
class DemoController extends Controller
* Write code on Method
* @return response()
public function index()
$visitors = Visitor::select(
DB::raw("(sum(click)) as total_click"),
DB::raw("(DATE_FORMAT(created_at, '%Y')) as my_year")
 => Array
[id] => 1
[total_click] => 29
[my_year] => 2020
 => Array
[id] => 3
[total_click] => 101
[my_year] => 2021
i hope it can help you...
We are Recommending you
- Laravel Eloquent Group By with Month and Year Example
- Laravel Eloquent Group By Example
- How to Group By with Order By Desc in Laravel?
- Laravel Collection GroupBy with Examples
- Laravel 5.5 group by doesn't work - fixed
- How to use groupby having with DB::raw in Laravel Query Builder?
- Group by year month example in Laravel using Query Builder
- How to group by multiple columns in Laravel Query Builder?
- Laravel Select with Count Query with Group by Example
- GROUP_CONCAT with different SEPARATOR in laravel Example