Laravel Eloquent "WHERE NOT IN"

Cover Image for Laravel Eloquent "WHERE NOT IN"
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📚 Laravel Eloquent "WHERE NOT IN" 🚫

Are you having trouble writing queries in Laravel Eloquent ORM? Don't worry, we've got you covered! In this blog post, we'll walk you through converting a common SQL query into Laravel Eloquent, specifically focusing on the "WHERE NOT IN" clause.

The Challenge 😰

One of our readers faced a challenge when trying to convert the following SQL query into Laravel Eloquent:

SELECT book_name, dt_of_pub, pub_lang, no_page, book_price  
FROM book_mast        
WHERE book_price NOT IN (100, 200);

The goal was to retrieve book details excluding those with a book price of 100 or 200.

The Solution 💡

To achieve the same result using Laravel Eloquent, you can leverage the whereNotIn method. Let's see how it works:

$books = DB::table('book_mast')
           ->select('book_name', 'dt_of_pub', 'pub_lang', 'no_page', 'book_price')
           ->whereNotIn('book_price', [100, 200])
           ->get();

In the above code snippet, we use the DB facade to access the table book_mast. Next, we select the desired columns using the select method. Finally, we apply the whereNotIn method to exclude books with a price of 100 or 200.

🎉 Pro Tip: Use Eloquent Models 🏆

While the above solution works perfectly fine, we always recommend using Eloquent models for a more expressive and object-oriented approach. Here's how you can achieve the same result using an Eloquent model:

$books = BookMast::select('book_name', 'dt_of_pub', 'pub_lang', 'no_page', 'book_price')
                 ->whereNotIn('book_price', [100, 200])
                 ->get();

By defining a BookMast Eloquent model and using it in your query, your code becomes more elegant and readable.

Conclusion 🎓

In this blog post, we demonstrated how to convert a SQL query with a "WHERE NOT IN" clause into Laravel Eloquent. We covered both the basic solution using the DB facade and the recommended approach using Eloquent models.

Now that you have learned this handy technique, try applying it to your own projects and simplify your queries. Happy coding! 😊

Have any questions or suggestions? Leave a comment below and let's discuss! 👇


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello