Home » Laravel Eloquent WhereIn Method: 4 Real Life Examples

Laravel Eloquent WhereIn Method: 4 Real Life Examples

Loved it? Please Share on Social Media

Introduction:

Hey there! Welcome to my new tutorial about laravel eloquent wherein clause. In this tutorial, we are going to learn as well as practice how wherein clause works. We will experiment with wherein clauses to understand where we can use them and where we cannot.
Laravel eloquent wherein clause is used to find data from an array or object. It accepts two parameters. The first parameter is the table column name, and the second is an array or an object. It compares with the given array or object on the table column name; if found, it returns the final data.

Live Examples of laravel eloquent wherein Method:

Now, we will experiment with the wherein clause; we already have a fresh copy of the laravel application installed on our machine. Please try to look at and practice the following code snippet for a better understanding. Let’s try it.

$users = User::whereIn('id', [1, 3, 6, 9])->get();
dd($users);

As you can see from the upper example, we are trying to fetch those users according to their IDs, right?? After that, we must dump and die that $users variable to know what it returns; it returns that particular user with an ID of 1, 3, 6, and 9, which is very simple.
OK, Now let’s look at a new example where we will also use the wherein method to fetch data.

$orders = Order::whereIn('status', ['Approved', 'Declined', 'Pending'])
->where('user_id', 1)
->get();
dd($orders);

From the upper example, we determine how the wherein clause works here; I’ve added some twists by adding a general where clause. It returns orders with states like ‘Approved,’ ‘Declined,’ and ‘Pending’ and also for user id 1.
Here is another example of wherein you may look at it.

$states = State:: whereIn('country_id', [1, 4, 6, 8])->orderBy('country_id') ->get();
dd($states);

Lastly, we will look at a post model using laravel eloquent wherein clauses are used to find selected posts according to their IDs. We can also use DB Facade interaction with the wherein clause.

$posts = DB::table('posts')->whereIn('id', [89, 23, 90])->get();
dd($posts);

Quick Notes About laravel eloquent wherein Method:

Optionally, in laravel, wherein clause has two extra parameters, you may want to pass it on to that clause. If we use the third parameter as a and & if we use the fourth parameter as true it will work the same as whereNotIn clause. Another is If we use the third parameter as or and the fourth parameter is false, then it will work like the orWhereIn clause. Now it’s up to you whether you want to use it or not it’s totally up to you. I hope it will make sense.

Tips regarding laravel eloquent wherein Mehod:

If we have an array of big data, the wherein clause will slow in the query. That’s why we should always try to give very small data like 10, 50, and 100. Maybe even more data will be a good start for querying using the laravel eloquent wherein clause. you can check their documentation also.

Conclusion:

That’s all for today! We have learned how laravel eloquent wherein clause works and how it works. We have also learned step by step how we can implement the wherein clause in our application as per requirements. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little, Please share this post on social media. If you have any questions or suggestions regarding the post you can contact us via our Contact Us page.

FAQ’s about laravel eloquent wherein Method

What is the difference between where and whereIn in Laravel?

In brief, the whereIn method takes just one query value while agreeing with an array or object value.

What kind of parameters are required in laravel eloquent whereIn method?

In Laravel, wherein the clause supports four parameters, the first two are required, and the other two are optional. The first parameter is necessary for column_name, and the second is the array or object.

What is the WhereIn clause in Laravel?

Laravel whereIn is just an eloquent method for Database Operation. The Laravel whereIn clause compares column values from an array or object.

How to use wherein in SQL?

WHERE IN is a SQL keyword that specifies multiple values in a WHERE clause; it allows us to specify a list of values to search in a specific field. For instance, a simple wherein query might be like below

SELECT * FROM orders WHERE status IN (‘Pending,’ ‘Approved,’ ‘Delivered’).


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.