How to get "select count (*) group" using laravel eloquent

I would like to execute the following sentence using laravel eloquent

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

The only solution for me is to create a view in the database, but I'm sure there is a way to do this with laravel.

+6
source share
2 answers

You can use this:

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

As you want to do this with Laravel Eloquent, I assume that you have the model name Reserve . In this case, you can use this

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

All Articles