senty
senty

Reputation: 12847

Using Array in '->where()' for Laravel Query Building

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?

Upvotes: 3

Views: 274

Answers (2)

Simhachalam Gulla
Simhachalam Gulla

Reputation: 996

Apply whereIn method

if(is_array($subscribes) && count($subscribes) > 0){
    $videos = Photo::whereIn('id', $subscribes)->get();
}

Upvotes: 1

Steve
Steve

Reputation: 20469

Use whereIn method:

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

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

Upvotes: 3

Related Questions