Laravel-9 Tutorial – Laravel 9 WhereIn: How to Use WhereIn Query 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


namespace AppHttpControllers;
use AppHttpControllersController;
use IlluminateSupportFacadesDB;
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


namespace AppHttpControllers;
use AppHttpControllersController;
use AppStudents;
class UserController extends Controller
{
    public function index()
    {
        $users = Students::table('students')
            -> whereIn('name', ['John', 'Randy', 'Harry'])
            ->get();
        return view('home', ['students' => $students]);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *