Laravel 5 select with count query with group by example

By Hardik Savani | June 9, 2016 | | 29171 Viewer | Category : Laravel


We always required to get count of number of records or number of inserted record of current month etc in laravel. We can use mysql count function in laravel eloquent. We have two way to get count of column value. first we can use laravel count() of query builder and another one we can use with directly with select statement using DB::raw(). I give you both example you can see and 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);



We are Recommending you: