Laravel whereNull and whereNotNull Query Example

Laravel whereNull and whereNotNull Query Example

Laravel where Null and where Not Null example. In this tutorial, you will learn how to use whereNull() and whereNotNull() eloquent methods to implementing a query in laravel apps.

As well as you will learn about whereNull and whereNotNull core SQL queries.

The following syntax represents the whereNull and whereNotNull eloquent methods:

whereNull

whereNull('columnName');

whereNotNull

whereNotNull('columnName');

It’s good always practice check null fields when building queries on models.

Example 1: Laravel whereNull Query

Using the following query, you can fetch data from database table users where the name field empty in DB table:

public function index()
{
    $users = User::whereNull('name')->get();
  
    dd($users);                    
}

When you dump the above given whereNull query you will get the following SQL query:

SELECT * FROM users WHERE name IS NULL;

Example 2: Laravel whereNotNull Query

Using the following query, you can fetch data from database table users where the email_verified_at field is not empty in DB table:

public function index()
{
    $users = User::whereNotNull('email_verified_at')->get();
  
    dd($users);                    
}

When you dump the above given whereNotNull query you will get the following SQL query:

SELECT * FROM users WHERE email_verified_at IS NOT NULL;

Conclusion

In this tutorial, you have learned how to use laravel where null and where not null eloquent method with query builder and model.

Recommended Laravel Posts

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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