How do I get a "select count(*) group by" using laravel eloquent

Ugo Guazelli picture Ugo Guazelli · Jan 16, 2016 · Viewed 42.3k times · Source

I would like to execute the follow sentence using laravel eloquent

SELECT *, count(*) FROM reserves  group by day

The only solution occurs to me is to create a view in the DB, but I am pretty sure there is a way to do it in laravel way.

Answer

Marcin Nabiałek picture Marcin Nabiałek · Jan 16, 2016

You could use this:

$reserves = DB::table('reserves')->selectRaw('*, count(*)')->groupBy('day');