3

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?

1
  • is it below one working? Commented Dec 16, 2015 at 12:43

2 Answers 2

3

Use whereIn method:

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

Apply whereIn method

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.