🎉🎉 Larasense is officially launched 🎉🎉
- Your Hub for Laravel News, Trends & Updates

Master Laravel Where Not Queries to Exclude Unwanted Data

laravel
tutorial
eloquent
Nabil Hassen
Nabil Hassen
Dec 13, 2024
Laravel where not queries
Last updated on Dec 12, 2024
Table of contents:

Understanding whereNot Queries in Laravel

whereNot queries in Laravel are used to fetch records that do not meet a specific condition. For example, if you want to retrieve all users except those with the role of "admin," you can use a whereNot query to achieve this.

These queries help you exclude data from your results, making it easy to focus only on the information you need.

All Types of whereNot Queries in Laravel

1. whereNot

This method excludes results based on a single condition. For instance:

Example: Exclude Users with a Specific Role

// these are equivalent to the whereNot method
$users = User::where('role', '!=', 'admin')->get();
$users = User::where('role', '<>', 'admin')->get();
 
$users = User::whereNot('role', 'admin')->get();

This query retrieves all users whose role column is not equal to "admin."

2. whereNotIn

If you need to exclude multiple values from your results, use whereNotIn.

Example: Exclude Multiple Roles

$users = User::whereNotIn('role', ['admin', 'editor'])->get();

This query excludes users with roles of either "admin" or "editor."

3. whereNotNull

To exclude records where a column is null, you can use whereNotNull.

Example: Fetch Users with Non-Empty Emails

$users = User::whereNotNull('email')->get();

This query retrieves all users who have an email address.

4. whereNull

Although not exactly a whereNot query, its complement whereNull can also be used effectively. For example:

Example: Fetch Users without an Email Address

$users = User::whereNull('email')->get();

This query retrieves all users whose email column is null.

5. whereNotBetween

If you need to exclude records that fall within a specific range, use whereNotBetween.

Example: Exclude Orders within a Date Range

$orders = Order::whereNotBetween('created_at', ['2024-01-01', '2024-12-31'])->get();

This query retrieves all orders outside the given date range.

6. whereNotExists

To exclude results based on a related query, use whereNotExists.

Example: Exclude Users Without Orders

$users = User::whereNotExists(function ($query) {
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})->get();

This query retrieves users who do not have any orders in the orders table.

7. whereNot with Closures

For more complex logic, you can use a closure inside a whereNot query.

Example: Exclude Users Based on Multiple Conditions

$users = User::whereNot(function ($query) {
$query->where('role', 'admin')
->orWhere('is_active', false);
})->get();

This query retrieves all users who are not admins and are also active.

Note that all of the above where not methods could be used in an "OR" condition such as orWhereNot, orWhereNotIn, etc.

Combining whereNot with Other Conditions

You can combine whereNot queries with other methods to refine your results. For instance:

Example: Fetch Active Users Excluding Admins

$users = User::whereNot('role', 'admin')
->where('is_active', true)
->get();

This query retrieves all active users whose role is not "admin."

Wrapping Up

whereNot and its related methods (whereNotIn, whereNotNull, whereNotBetween, whereNotExists, and whereNot with closures) give you powerful tools to exclude unwanted data from your queries. Understanding and mastering these methods will help you write cleaner and more efficient Laravel code.

Nabil Hassen
Nabil Hassen
Full Stack Web Developer

Stay Updated.

I'll you email you as soon as new, fresh content is published.

Thanks for subscribing to my blog.

Latest Posts