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.
When querying your Laravel Eloquent you can directly chain the "whereNotNull()" from the model itself.
Laravel Eloquent whereNotNull
When querying your Laravel Eloquent you can directly chain the "whereNotNull()" from the model itself.
<?php
$posts = Post::query()
->whereNotNull('published_at')
->get();Laravel Query Builder whereNotNull
If you do prefer using the "DB" facade then you can write your code with the "whereNotNull()" condition like below.
<?php
$posts = DB::table('posts')
->whereNotNull('published_at')
->get();Inverse of whereNotNull
The inverse of "whereNotNull()" is "whereNull()" and this method will check if the value is NULL.
<?php
Post::query()
->whereNull('published_at')
->get();
DB::table('posts')
->whereNotNull('published_at')
->get();