Patrick Obafemi
Patrick Obafemi

Reputation: 1066

count get collection and count query giving different results

So i am trying to get distinct collections like this

$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->get();

But when i do a count($data) I get 1600 but when i run

$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->count();

I get 1440. This is weird as i only want collection with distinct field 'Farmer_BankVerificationNumber_Farmer'. How do i write the query correctly?

Upvotes: 1

Views: 404

Answers (1)

Super_Simon
Super_Simon

Reputation: 1296

It's because you're asking your query to count the distinct values in the wrong place.

You need to tell your count parameter what field you would like to count on. So you'll basically ask the query to get the database information, separate the distinct values and then count how many of a specific field are distinct.

Your finished query should look like this:

$data = DB::table('project_1_data')
    ->distinct()
    ->count('Farmer_BankVerificationNumber_Farmer');

Upvotes: 3

Related Questions