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

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

by Shahriar Sagor

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.

close

You may also like