mardi 26 janvier 2021

How to use multiple whereIn in laravel collection

I have a collection of arrays that I'm trying to return based on the $prices value that I passed in. $prices = ['100', '200'];

 $collection = collect([
   ['product' => 'Desk', 'price' => 100, 'price2' => 150],
   ['product' => 'Chair', 'price' => 150, 'price2' => 200],
   ['product' => 'Bookcase', 'price' => 300, 'price2' => 350],
   ['product' => 'Door', 'price' => 400, 'price2' => 200],
   ['product' => 'Door', 'price' => 450, 'price2' => 500],
 ]);

 $filtered = $collection->whereIn('price', $prices)->whereIn('price2', $prices);

 $filtered->all();

/* The result should return where price or price2 is 100 or 200:
  [
    ['product' => 'Desk', 'price' => 100, 'price2' => 150],
    ['product' => 'Chair', 'price' => 150, 'price2' => 200],
    ['product' => 'Door', 'price' => 400, 'price2' => 200],
  ]
*/


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire