You can use filter
, like so:
$desired_object = $food->filter(function($item) {
return $item->id == 24;
})->first();
filter
will also return a Collection
, but since you know there will be only one, you can call first
on that Collection
.
You don't need the filter anymore (or maybe ever, I don't know this is almost 4 years old). You can just use first
:
$desired_object = $food->first(function($item) {
return $item->id == 24;
});