mardi 19 février 2019

Select where all 3 columns are not null at the same time (Laravel Query Builder)

There is articles DB table (and Article model) that has (among others) these 3 columns:

  • col_1
  • col_2
  • col_3

How to select all of the articles where these 3 columns are not NULL at the same time?

So, if col_1 = NULL and col_2 = NULL and col_3 = NULL - do NOT select (include) those articles. But if one or more of these columns is NOT NULL - then select (include) it.

The query builder for this would look like this:

Article::select('articles.*')-> ... ->get();

Of course, instead of ... there would be that check if all of these 3 columns are not null at the same time.

I know that this is wrong:

Article::select('articles.*')
       ->whereNotNull('col_1')
       ->whereNotNull('col_2')
       ->whereNotNull('col_3')
       ->get();

... because it will not select (include) the articles where, for example, one is NULL (col_1) and the rest (col_2 and col_3) are not NULL.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire