Understanding whereNot: Excluding Records
The whereNot method is perfect when you want to exclude records that match certain conditions. Think of it as saying "give me everything EXCEPT..."
Basic whereNot Usage
$products = DB::table('products')
->whereNot(function (Builder $query) {
$query->where('clearance', true)
->orWhere('price', '<', 10);
})
->get();
This query finds products that are not on clearance and don’t have a price less than $10. It’s like telling the database: “Show me products, but skip the clearance items and anything under $10.”
Real-World whereNot Example
// Find active users who are NOT in restricted countries
$users = DB::table('users')
->where('status', 'active')
->whereNot(function ($query) {
$query->whereIn('country', ['blocked_country_1', 'blocked_country_2']);
})
->get();
whereAny: Flexible Column Matching
The whereAny method searches across multiple columns with the same condition. It's incredibly useful for search functionality where you want to find a term in any of several fields.
Basic whereAny Usage
$users = DB::table('users')
->where('active', true)
->whereAny([
'name',
'email',
'phone',
], 'like', 'John%')
->get();
This searches for active users where any of the name, email, or phone fields start with “John”. The resulting SQL uses OR conditions:
SELECT * FROM users
WHERE active = true AND (
name LIKE 'John%' OR
email LIKE 'John%' OR
phone LIKE 'John%'
)
Practical whereAny Example
// Search for products in multiple description fields
$products = DB::table('products')
->where('status', 'published')
->whereAny([
'title',
'description',
'tags'
], 'like', '%wireless%')
->get();
whereAll: Strict Column Matching
When you need all specified columns to match a condition, whereAll is your friend. It ensures every listed column meets your criteria.
Basic whereAll Usage
$posts = DB::table('posts')
->where('published', true)
->whereAll([
'title',
'content',
], 'like', '%Laravel%')
->get();
This finds published posts where both the title and content contain “Laravel”. The SQL uses AND conditions:
SELECT * FROM posts
WHERE published = true AND (
title LIKE '%Laravel%' AND
content LIKE '%Laravel%'
)
Real-World whereAll Example
// Find products with consistent pricing across all regions
$products = DB::table('products')
->whereAll([
'us_price',
'eu_price',
'asia_price'
], '>', 0)
->get();
whereNone: Excluding All Matches
The whereNone method ensures that none of the specified columns match your condition. It's perfect for filtering out unwanted content.
Basic whereNone Usage
$albums = DB::table('albums')
->where('published', true)
->whereNone([
'title',
'lyrics',
'tags',
], 'like', '%explicit%')
->get();
This finds published albums where none of the title, lyrics, or tags contain “explicit”. The SQL uses NOT with OR:
SELECT * FROM albums
WHERE published = true AND NOT (
title LIKE '%explicit%' OR
lyrics LIKE '%explicit%' OR
tags LIKE '%explicit%'
)
Practical whereNone Example
// Find family-friendly content
$movies = DB::table('movies')
->where('status', 'released')
->whereNone([
'title',
'description',
'genre'
], 'like', '%horror%')
->get();
Combining Methods for Complex Queries
These methods become even more powerful when combined:
$products = DB::table('products')
->where('status', 'active')
->whereAny(['name', 'description'], 'like', '%phone%')
->whereNone(['tags', 'category'], 'like', '%discontinued%')
->whereNot(function ($query) {
$query->where('price', 0)
->orWhere('stock', '<', 1);
})
->get();
This complex query finds:
- Active products
- With “phone” in name or description
- Without “discontinued” in tags or category
- That aren’t free or out of stock
Performance Tips
- Use indexes: Make sure columns used in these methods are properly indexed
- Limit results: Add
limit()when you don't need all matching records - Select specific columns: Use
select()to fetch only needed data
Conclusion
- whereNot: Exclude specific conditions or groups of records
- whereAny: Search across multiple columns (great for search features)
- whereAll: Ensure all columns meet criteria (strict filtering)
- whereNone: Filter out unwanted content across multiple fields
These advanced where clauses make your Laravel queries more expressive and maintainable. Instead of writing complex SQL with multiple joins and subqueries, you can express your intent clearly using Laravel’s fluent interface.
By mastering these methods, you’ll write cleaner, more readable code that’s easier for your team to understand and maintain.
If you found this helpful, feel free to share or drop a comment. Happy coding with Laravel! 🧱✨
Free resources for this topic
Code snippets and fixes for this topic
Get one practical tip every week
New tutorials, fixes and free cheat sheets from DEV Talk, straight to your inbox. No spam, unsubscribe in one click.
Free download: Laravel interview questions (PDF)
Have a question about this post?
Ask the community: other developers and the author can answer.
Get notified about new articles
One click, no email needed. Turn it off anytime with the bell at the top.
Get new articles on your iPhone
Add DEV Talk to your Home Screen, open it, then tap the bell.
Enjoying the content?
If this article helped you, you can support the author with a coffee. It keeps new posts coming.
Comments 0
No comments yet
Be the first to share your thoughts.