Laravel Distinct Count

Tags: , , ,

Any way to make this query work using laravel? DB::raw or Eloquent usage doesn’t matter.

SELECT count(DISTINCT name) FROM tablename;

Here’s what i’ve tried but cannot get the proper output:

EloquentTableName::select(DB::raw('count(DISTINCT name) as name_count'))->get();

This returns something like this and i’d like to fix that:


I just want to get count 15.


you can simply replace get with count in this way:

$count = DB::table('tablename')->count(DB::raw('DISTINCT name'));

also can do:


Source: stackoverflow