Laravel Wherein Query Example

Laravel Wherein Query Example

In this post we will give you information about Laravel Wherein Query Example. Hear we will give you detail about Laravel Wherein Query ExampleAnd how to use it also give you demo for it if it is necessary.

Today’s topic is how to use wherein query in laravel eloquent. How works laravel model wherein array query. i will give you simple example of wherein laravel query builder. you can use laravel eloquent where in array.

If you need to use sql wherein query in laravel then you can use with array. Laravel provide wherein() to use sql wherein query. in wherein() we just need to pass two argument one is column name and another if array of ids or anything that you want.

You can see bellow syntax on wherein query in laravel:

whereIn(Coulumn_name, Array);

Now i will give you three example of how to use wherein query in laravel application. So let’s see those example how it works.

Example 1:

SQL Query:

SELECT *

FROM users

WHERE id IN (4, 5, 6)

Laravel Query:

public function index()

{

$users = User::select("*")

->whereIn('id', [4, 5, 6])

->get();

dd($users);

}

Example 2:

Here is another example of wherein query. You can work with comma separated string value. you can work as like bellow:

public function index()

{

$myString = '1,2,3';

$myArray = explode(',', $myString);

$users = User::select("*")

->whereIn('id', $myArray)

->get();

dd($users);

}

Example 3:

Also see:How to Create and Use Query Scope in Laravel Eloquent

public function index()

{

$users = DB::table('users')

->whereIn('name', ['Hardik', 'Vimal', 'Harshad'])

->get();

dd($users);

}

I hope it can help you…

Hope this code and post will helped you for implement Laravel Wherein Query Example. 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

Leave a Comment

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

5  +  5  =  

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