laravel collection has value code example
Example 1: laravel check if item is in collection
$collection = collect([
['product' => 'Desk', 'price' => 200],
['product' => 'Chair', 'price' => 100],
]);
$collection->contains('product', 'Bookcase');
Example 2: collection laravel filter
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
});
$filtered->all();
Example 3: laravel check if collection has value
Auth::user()->ports->contains('port', $request->port);
Example 4: laravel reduce
$collection = collect([1, 2, 3]);
$total = $collection->reduce(function ($carry, $item) {
return $carry + $item;
});
$total = $collection->reduce(function ($carry, $item) {
return $carry + $item;
}, 4);