Laravel Subquery in WhereIn() and WhereNotIn() Example Tutorial

May 15, 2024 | Laravel 10 MySql Laravel


Hello Dev,

"Laravel, being one of the most popular PHP frameworks today, emphasizes efficient database interactions. One crucial aspect is utilizing subqueries with the `whereIn` and `whereNotIn` clauses. These are powerful tools for filtering data based on sets of values.

There are instances where we need to employ a select query with `whereIn` or `whereNotIn` clauses in Laravel versions 6 through 11. While it's straightforward to write such queries in MySQL, translating them into Laravel's query builder might not be immediately obvious. In this example, I'll demonstrate how to convert a MySQL query into Laravel's query builder syntax, specifically for `whereIn` with a subquery.

Let's start by understanding the tables involved in this example:"

It seems like your question got cut off before listing the tables. However, I can still provide a general guide on how to use subqueries with whereIn and whereNotIn clauses in Laravel, which applies to Laravel versions 6 through 11. Let's assume you have two tables, orders and customers, and you want to filter orders based on whether their IDs exist in a list of customer IDs.

Step 1: Define Your Tables

Assuming you have the following tables:

Orders Table

id
customer_id
order_date

Customers Table

id
name
email

Read Also: Bootstrap PHP Contact Form With Validation Example Tutorial Step 2: Convert MySQL Query to Laravel Query Builder

Let's say you have a MySQL query that selects orders where the customer_id exists in a list of customer IDs:

SELECT * FROM orders WHERE customer_id IN (1, 2, 3);

And another query that selects orders where the customer_id does not exist in a list of customer IDs:

SELECT * FROM orders WHERE customer_id NOT IN (1, 2, 3);
Step 3: Using Subqueries with whereIn and whereNotIn in Laravel Using whereIn

To achieve the equivalent of the first SQL query in Laravel, you can use the whereIn method along with a subquery:

 
$customerIdList = [1, 2, 3]; // List of customer IDs

$orders = DB::table('orders')
    ->whereIn('customer_id', function ($query) use ($customerIdList) {
        $query->select('id')->from('customers')->whereIn('id', $customerIdList);
    })
    ->get();
Read Also: Jquery Datatables Server Side Processing PHP Example Tutorial Using whereNotIn

Similarly, for the second SQL query, you can use the whereNotIn method:

 
$customerIdList = [1, 2, 3]; // List of customer IDs

$orders = DB::table('orders')
    ->whereNotIn('customer_id', function ($query) use ($customerIdList) {
        $query->select('id')->from('customers')->whereIn('id', $customerIdList);
    })
    ->get();
Explanation

Subquery: The anonymous function passed to whereIn or whereNotIn acts as a subquery. It specifies the condition for selecting IDs from the customers table.
Using Variables: The use keyword is used to import variables from the outer scope into the closure's scope. This allows you to pass the $customerIdList array to the subquery.
Retrieving Results: Finally, calling get() executes the query and retrieves the results.

These examples demonstrate how to translate MySQL queries involving IN and NOT IN with subqueries into Laravel's query builder syntax, providing a powerful tool for filtering data based on conditions defined within subqueries.

Thank you for your encouragement! If you have any questions or need further assistance, feel free to ask. I'm here to help!



Tags :
#Laravel 10
#MySql
#Laravel
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"