Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Laravel: how to use whereIn / orwhereIn to fetch records with Laravel?

While developing one of my project in Laravel framework, I have shared a fine piece of code on community portal stackoverflow, which I wish share as below. Hope it will help devs

http://stackoverflow.com/questions//laravel-wherein-or-wherein/#

 

user-image
Question added by Abdul Rehman , Technical Leader , Xipetech
Date Posted: 2016/07/03
Rashedul Hoque
by Rashedul Hoque , Sr. Software Developer , Orko Health Ltd.

Almost2 years have passed, hope that you have already found a way to use wherein and orwherein in your application.

I am answering this because it may help others.

Assuming you know the eloquent queries :

If we have a model called User and we want to find list of users having id2,,

We can find it using following query

$users = User::whereIn('id', [2,,])->get();

Now if you want to find all the records having id2,, or1,,

Then you have to combined both whereIn and orWhereIn , there is a way to do this :

$users = User::whereIn('id', [2,,])->whereIn('id',[1,,],'or')->get();

You may be also interested in whereNotIn method which is desined to filters the collection by a given key / value not contained within the given array:

Hope this answer is useful.

Thank you

More Questions Like This