How to use whereIn Query in Laravel?

How to use whereIn Query in Laravel?

In this post we will give you information about How to use whereIn Query in Laravel?. Hear we will give you detail about How to use whereIn Query in Laravel?And how to use it also give you demo for it if it is necessary.

In this article, i will share with you very simple but most used laravel query example how to use whereIn query in laravel eloquent. I will give you a very simple example of how to use whereIn in laravel.

If you want to match one record id given by id’s array then you should be used whereIn for done these tasks.

Syntax

whereIn(Coulumn_name, Array);

Simple SQL Query

SELECT *
  	FROM articles
  	WHERE id IN (2,3,1)

Now, we will see how to use in laravel application in many ways

Example – 1

In this example, we will simply get some articles from the given by categories array.

public function index()
{
    $articles = Article::select("*")
        ->whereIn('category_id', [4, 5, 6])
        ->get();
  
    dd($articles);                    
}

Example -2

In this example, i will show you how to match comma-separated string values in whereIn().

public function index()
{
	$categoryIdString = '2,3,4';
    $categoryIds = explode(',', $categoryIdString);

    $articles = Article::select("*")
        ->whereIn('category_id', $categoryIds)
        ->get();
  
    dd($articles);                    
}

Example -3

You can also use a string array with whereIn().

public function index()
{
    $articles = Article::select("*")
        ->whereIn('tag_name', ['laravel', 'python', 'react', 'anguler', 'node'])
        ->get();
  
    dd($articles);                    
}

i hope it will be help a lot.

Hope this code and post will helped you for implement How to use whereIn Query in Laravel?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US