P
P
Pavel Zhukau2015-08-16 17:40:05
Laravel
Pavel Zhukau, 2015-08-16 17:40:05

How to use where on a collection in a loop?

Good afternoon. The bottom line is, there is a variable - it contains a collection of $clothes_col.
And there is another variable $categories_id - an array that stores id, for example 1,2,3.
If I use this code, I get an error that I requested 1 item, and I have 0 of them.

foreach($categories_id as $category){
                $category += 0;
                $category_clothe = $clothes_col->where('category_id', $category)->get()->random()->toArray();
                print_r($category_clothe);
                echo "<br />";
            }

And if in where to write a numeric value instead of $category (1 or 2 or 3), then everything works.
Why do I need this: I have a collection and an array of id, and I need to get a new collection in which each element corresponds to some id from the array, as in my code (which does not work :( ) .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-08-16
@NikesDark

You seem to be using the get method inappropriately .
This is how it should work:
What kind of horror? You either type normally, or use whereLoose

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question