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

0 votes

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.

Oct 21 in Laravel by kartik
• 35,420 points
170 views

1 answer to this question.

0 votes

Hello @kartik,

You could use this:

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

Hope it helps!!

Thank you!!

answered Oct 21 by Niroj
• 76,780 points

More like:

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

if addidtional param with count is needed, or if just count is needed:

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

all explained in documentation (https://laravel.com/docs/8.x/queries#raw-expressions and https://laravel.com/docs/8.x/eloquent#retrieving-aggregates)


Related Questions In Laravel

0 votes
1 answer

How do I write to the console from a Laravel Controller?

Hello @kartik, This can be done with the ...READ MORE

answered Aug 11 in Laravel by Niroj
• 76,780 points
93 views
0 votes
1 answer

How do I get HTTP Request body content in Laravel?

Hello @kartik, Inside controller inject Request object. So ...READ MORE

answered Aug 11 in Laravel by Niroj
• 76,780 points
950 views
0 votes
1 answer

How to update a pivot table using Eloquent in laravel 5?

Hello @kartik, The code below solved my problem: $messages ...READ MORE

answered Sep 24 in Laravel by Niroj
• 76,780 points
221 views
0 votes
1 answer

How do I get raw form data in laravel?

Hii @kartik, Laravel intercepts all input. If you're ...READ MORE

answered Oct 28 in Laravel by Niroj
• 76,780 points
51 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27 in PHP by Niroj
• 76,780 points
176 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27 in PHP by Niroj
• 76,780 points
105 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27 in PHP by Niroj
• 76,780 points
157 views
0 votes
1 answer

What are the differences between mysqli_connect and mysqli_pconnect?

Hello, mysqli_pconnect() function is used for making a persistence ...READ MORE

answered Mar 27 in PHP by Niroj
• 76,780 points
403 views
0 votes
1 answer

How can we create a record in Laravel using eloquent?

We need to create a new model ...READ MORE

answered Mar 20 in Laravel by Niroj
• 76,780 points
137 views
0 votes
1 answer

How do I set up phpMyAdmin on a Laravel Homestead box?

Hello, Step 1: Go to the phpMyAdmin website, download the latest ...READ MORE

answered Mar 31 in Laravel by Niroj
• 76,780 points
1,275 views