Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count with Condition in laravel Eloquent

I want to count users who have stripe_plan = platinum_monthly

$platinum=User::count('stripe_plan','platinum_monthly');

But its returning all users who have selected any available plan how to solve this issue need help?

like image 246
Mirza Obaid Avatar asked Jun 05 '16 07:06

Mirza Obaid


People also ask

How to count eloquent laravel?

In Laravel eloquent or query builder we use count method to count the rows. We can also count rows after executing the query using get() or all() and then can apply collection count method.

What is count in laravel?

count is a Collection method. The query builder returns an array. So in order to get the count, you would just count it like you normally would with an array: $wordCount = count($wordlist); If you have a wordlist model, then you can use Eloquent to get a Collection and then use the Collection's count method.

How to calculate total in laravel?

$total = Model::sum('balance'); Note that it completely depends on your use case in the end.


2 Answers

Try adding the condition in a where clause

$platinum=User::where('stripe_plan','platinum_monthly')->count();
like image 120
jfadich Avatar answered Sep 23 '22 20:09

jfadich


$platinum = User::where('stripe_plan', 'platinum_monthly')->count();

You have to select the users you want to count, not "count the users you want to count".

like image 33
AntoineB Avatar answered Sep 24 '22 20:09

AntoineB