Posts Learn Components Snippets Categories Tags Tools About

How to Query Column That is Not Null In Laravel

Learn how to query a column that's NOT NULL in Laravel the easy way. Make use of this query directive to get the result as necessary.

Created on Oct 08, 2021


To query a column that's not null in Laravel, you can make use of the "whereNotNull()" function available to every query builder. By using this function you instruct the query builder to check against the NULL value of the database so you will get the result directly as you intended it to be.

Laravel Eloquent whereNotNull

When querying your Laravel Eloquent you can directly chain the "whereNotNull()" from the model itself.

$posts = Post::query()

Laravel Query Builder whereNotNull

If you do prefer using the "DB" facade then you can write your code with the "whereNotNull()" condition like below.

$posts = DB::table('posts')

Inverse of whereNotNull

The inverse of "whereNotNull()" is "whereNull()" and this method will check if the value is NULL.



If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️


PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Sponsors 👑

+ Add Yours