Home » Php » How do I get a “select count(*) group by” using laravel eloquent

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

You could use this:

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

Questions:
Answers:

As you wish to do it with Laravel Eloquent I assume you have a model name Reserve. In this case you can use this

$reserve = Reserve::all()->groupBy('day')->count();