Ad

Using Array In '->where()' For Laravel Query Building

- 1 answer

I am trying to get all values of a column, and then use it in another query. I used lists and got the values as an array but, I couldn't find the way to check that variable (array containing each id) in other query

$subscribes = Subscribe::where('from_id', $currentUser)->lists('id')

// dd($subscribes), logs values as array.


$videos = Photo::where('id', $subscribes)->get();

This doesn't work because $subscribes is an array.

Should I use a for loop and send another query for each id? Or is there a practical way that I am missing out? What is the proper way of using it?

Ad

Answer

Use whereIn method:

$videos = Photo::whereIn('id', $subscribes)->get();

http://laravel.com/docs/5.1/queries (scroll down / search 'whereIn')

Ad
source: stackoverflow.com
Ad