DEV Community

Cover image for Laravel 8 wherein() Query Example
Code And Deploy
Code And Deploy

Posted on

Laravel 8 wherein() Query Example

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/laravel-8-wherein-query-example

In this post, I'm sharing how to use Laravel 8 whereIn() query from Eloquent and query builder. This method will able us to search many values in a single query using array values. I will provide an example for the Laravel whereIn() method so that it will be easier for you to understand and apply to your project.

The method whereIn() can be applied in the following versions of Laravel 6, Laravel 7, and Laravel 8.

How does It work?

The below sample code using whereIn() method the first parameter is your field_name target and the second parameter will be an array of what you need to search or query.

wherein('field_name', $array_variable)
Enter fullscreen mode Exit fullscreen mode

Now you have the basics of how it works. Let's try to do it with an example.

SQL Query Example:

SELECT *
  FROM posts
  WHERE id IN (1, 2, 3) 
Enter fullscreen mode Exit fullscreen mode

WhereIn Query with Laravel 8 Query Builder

Wherein query example with IDs into an array.

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        $posts = DB::table('posts')
            ->whereIn('id', [1, 2, 3])
            ->get();

        print_r($posts);die;
    }
}
Enter fullscreen mode Exit fullscreen mode

Wherein query example with post title into an array.

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        $posts = DB::table('posts')
            ->whereIn('title', ['post1', 'post2', 'post3'])
            ->get();

        print_r($posts);die;
    }
}
Enter fullscreen mode Exit fullscreen mode

WhereIn Query with Laravel 8 Eloquent

Wherein query example with IDs into an array.

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        $posts = Post::whereIn('id', [21, 22, 23])->get();

        print_r($posts);die;
    }
}
Enter fullscreen mode Exit fullscreen mode

Wherein query example with post title into an array.

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        $posts = Post::whereIn('title', ['post1', 'post2', 'post3'])->get();

        print_r($posts);die;
    }
}
Enter fullscreen mode Exit fullscreen mode

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-wherein-query-example if you want to download this code.

Top comments (0)