29.07.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Collections 212<br />

.<br />

1 $random = $collection->random(3);<br />

2<br />

3 $random->all();<br />

4<br />

5 // [2, 4, 5] - (retrieved randomly)<br />

reduce() {#collection-method}<br />

The reduce method reduces the collection to a single value, passing the result of each iteration into<br />

the subsequent iteration:<br />

.<br />

1 $collection = collect([1, 2, 3]);<br />

2<br />

3 $total = $collection->reduce(function ($carry, $item) {<br />

4 return $carry + $item;<br />

5 });<br />

6<br />

7 // 6<br />

The value for $carry on the first iteration is null; however, you may specify its initial value by<br />

passing a second argument to reduce:<br />

.<br />

1 $collection->reduce(function ($carry, $item) {<br />

2 return $carry + $item;<br />

3 }, 4);<br />

4<br />

5 // 10<br />

reject() {#collection-method}<br />

The reject method filters the collection using the given callback. The callback should return true<br />

for any items it wishes to remove from the resulting collection:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!