WhereNotNull

WhereNotNull method in Laravel 5.0 or above

Hey, if you are searching about WhereNotNull() in laravel, I’m here to describe this to you today,

If you are suppose to find/fetch whether a field is NOT NULL in DB, it is possible to check that in Eloquent ORM in laravel.

Suppose you want to select all the results where notify_me column values are not null. In Laravel version >= 5.*, you can write it as given below

Using Query Builder Method

DB::table('users')->whereNotNull('notify_me')->get();

or

Using Eloquent ORM Method

Users::whereNotNull('notify_me');

whereNotNull() is a default method of Laravel.

There is one another method called WhereNull(). It can be used to check whether field is null or not. For example, if you want to check the null in the last_name field you can use WhereNull().

Using Query Builder Method

 DB::table('users')->whereNull('last_name')->get();

Using Eloquent ORM Method

Users::whereNull('last_name');

Leave a Reply

Your email address will not be published. Required fields are marked *