DEV Community

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

Posted on

2 1

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.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay