How to Use Group by year month using Laravel Query Builder ?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now here i will talk about Group by year month example in Laravel using Query Builder. Many of times, we need to created_at column with timestamp and also we have to get data with group by with month, on that way we are use for create chart. However, we can do this by using mysql MONTH() function. So we can also use DB raw with mysql function and also get by group by monthly records.

So, just you can see in the bellow query and also you use it.

Example:
DB::table("clicks")
	->select("id" ,DB::raw("(COUNT(*)) as total_click"))
        ->orderBy('created_at')
        ->groupBy(DB::raw("MONTH(created_at)"))
        ->get();

Read Also :Laravel Join with Subquery in Query Builder Example

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →