Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

search in collection laravel

Tags:

php

laravel-5

I have a very large collection of 150 objects

 ...
 App\almacen {#1679
     id: 124,
     emp_id: 1,
     dst_id: 13,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
   App\almacen {#1680
     id: 125,
     emp_id: 1,
     dst_id: 11,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
   App\almacen {#1681
     id: 126,
     emp_id: 1,
     dst_id: 12,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
   App\almacen {#1682
     id: 127,
     emp_id: 1,
     dst_id: 20,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
   App\almacen {#1683
     id: 128,
     emp_id: 1,
     dst_id: 7,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
  ...

and I have to compare if the keys emp_id and dst_id are equal to a number for example 8 and return 'true' if it exists Is there any way to do this without using 'foreach' and looking directly at the object? already use contains (), search () but always return 'false' even if the value does exist

like image 303
jhoss Avatar asked Oct 15 '25 20:10

jhoss


1 Answers

You can use the where method on collections like this:

$collection = collect([
    ['product' => 'Desk', 'price' => 200],
    ['product' => 'Chair', 'price' => 100],
    ['product' => 'Bookcase', 'price' => 150],
    ['product' => 'Door', 'price' => 100],
]);

$filtered = $collection->where('price', 100);

$filtered->all();
like image 199
ahmad_mhm Avatar answered Oct 18 '25 09:10

ahmad_mhm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!