Oscar
Oscar

Reputation: 139

How to count elements of a column ignoring the null values ​in Laravel 6.0

I'm trying to count all the elements of a column with Query Builder and I can't exclude null values ​​from the count, I tried this code but it didn't work.

$ttg = DB::table('incidencias')
                 ->select(DB::raw('count(*) as ttg, inc_padre'))
                 ->where('inc_padre', '<>', null)
                 ->groupBy('inc_padre ')
                 ->get();

Thanks in advance!

Upvotes: 0

Views: 1154

Answers (1)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

Try this one

$ttg = DB::table('incidencias')
    ->select(DB::raw('count(*) as ttg, inc_padre'))
    ->whereNotNull('inc_padre')
    ->groupBy('inc_padre')
    ->get();

For find all counts where inc_padre is not null

$ttg = DB::table('incidencias')
    ->whereNotNull('inc_padre')
    ->count();

Upvotes: 2

Related Questions