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.
DB::table("clicks") ->select("id" ,DB::raw("(COUNT(*)) as total_click")) ->orderBy('created_at') ->groupBy(DB::raw("MONTH(created_at)")) ->get();
Thanks for read. I hope it help you. For more you can follow us on facebook.