Home > Back-end >  How to get results based on elements in array with laravel whereIn?
How to get results based on elements in array with laravel whereIn?

Time:07-25

I have an array like this

$products = [350, 410, 362, 256, 193]

I have used this array to get results like this:

$searchResults = ProductSearch::whereIn('product_id', $products)->get();

In this result, I am not getting product with id 350 on first element in the result collection. Is there any way to achieve such result using ORM ?

CodePudding user response:

You need to return something from ProductSearch before using whereIn().

From the docs:

The whereIn method verifies that a given column's value is contained within the given array:

$users = DB::table('users')
                   ->whereIn('id', [1, 2, 3])
                   ->get();

You don't need to use ::table(), but something needs to be there.

CodePudding user response:

Have you checked that your table has a column( product_id ) available?

  • Related