Today now i this post i will talk about How to Select Count Query with Groupby In Laravel? Sometimes We need to get count of number of records or the number of the new visitors register last month or etc in laravel. We can do this by using mysql count function in laravel eloquent.There are two ways to get count of column value. The first one we can use laravel count() of query builder and another one we can use with the directly with select statement by using DB::raw().
Now here I will give you both example. We can see both of those and you use any one as perfect for you.
Example 1:
$data = DB::table("click")->count(); print_r($data);
Example 2:
$data = DB::table("click")
->select(DB::raw("COUNT(*) as count_row"))
->orderBy("created_at")
->groupBy(DB::raw("year(created_at)"))
->get();
print_r($data);
Read Also : Laravel 8 Mobile Number Verification Tutorial
Thanks for read. I hope it help you. For more you can follow us on facebook