kanu mahajan
kanu mahajan

Reputation: 109

How to count the number of records with groupby result in laravel?

I am getting stuck on this i want the count the number of records but it returns me wrong value every time .Here is my table:-

id  contributor_id  user_id     history_id  contributor_income 
1   1               14              1               0.57    
2   1               14              2               0.57    
3   1               14              3               0.57    
4   1               14              4               3.75    
5   2               14              5               0.57 

Here above is my table but it gives me count 4 but i want 2. Here is my query :-

$query  = DB::table('contributor_commissions')
          ->groupBy('contributor_id')->count();

But when i print this it gives count as 4 but there are only two groups of contributor_id

Upvotes: 2

Views: 2822

Answers (4)

N.S
N.S

Reputation: 139

Try this.

$query = $user_info = DB::table('contributor_commissions')
                     ->select('contributor_id', DB::raw('count(*) as total'))
                     ->groupBy('contributor_id')
                     ->get();

Upvotes: 1

Shanu k k
Shanu k k

Reputation: 1285

Please try this

DB::table('contributor_commissions')->distinct('contributor_id')->count('contributor_id');

Upvotes: 1

Nerea
Nerea

Reputation: 2157

This should work DB::table('contributor_commissions')->distinct('contributor_id')->count()

Upvotes: 1

CIZO
CIZO

Reputation: 4167

Here you go:

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

Upvotes: -1

Related Questions