Laravel Eloquent whereNull() & whereNotNull() Query
In this article, I’m going to share how to use whereNull()
and whereNotNull()
in Laravel application. Let’s see the usage:
Table of Contents
whereNull()
The whereNull()
helps us to get data with null values from database.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::whereNull('email_verified_at')->get();
dd($users);
}
}
whereNotNull()
The whereNotNull()
helps us to get data with not null values from database.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::whereNotNull('email_verified_at')->get();
dd($users);
}
}
That’s it. Thanks for reading. ?Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.