Laravel 10 WhereIn: How to Use WhereIn Query in Laravel

Last Updated on by in Laravel

In this tutorial, we are going to learn about laravel wherein query. We will know more about the nitty-gritty of wherein query in laravel eloquent. Basically, wherein array query makes sure if the provided column’s value exists inside the given array.

The laravel query builder makes your database query operations easy and gives you unbound experience while interacting with the database. The SQL wherein query is used with array and can be described with the wherein() method.

Read more: Laravel WhereNotIn Query Example

The Syntax

Here is the wherein query syntax, as you can see, it takes two parameters. One parameter is the column’s id, and the second is the array of id or the data that needs to be found.

whereIn(CoulumnName, Array);

Now, we are going to understand the two useful methods to write wherein query in laravel eloquent:

WhereIn Query with Laravel Query Builder

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{

    public function index()
    {
        $users = DB::table('products')
            -> whereIn('name', ['1', '3', '5'])
            ->get();

        return view('home', ['products' => $products]);
    }

}

WhereIn Query with Laravel Model

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Students;

class UserController extends Controller
{

    public function index()
    {
        $users = Students::table('students')
            -> whereIn('name', ['John', 'Randy', 'Harry'])
            ->get();

        return view('home', ['students' => $students]);
    }

}