use whereIn Query in Laravel?

use whereIn Query in Laravel?

In this post we will give you information about use whereIn Query in Laravel?. Hear we will give you detail about 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.

See also  How to delete multiple rows with checkbox using ajax in PHP Laravel 5.6 with example


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);                    
}


See also  Eloquent Query Scope in Laravel

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 and post will helped you for implement 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 Keep reading our blogs

See also  How to set vertical align middle div by CSS in HTML?

For More Info See :: laravel And github

Leave a Comment

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

87  +    =  90

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