29.07.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Collections 215<br />

.<br />

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

2<br />

3 $shuffled = $collection->shuffle();<br />

4<br />

5 $shuffled->all();<br />

6<br />

7 // [3, 2, 5, 1, 4] // (generated randomly)<br />

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

The slice method returns a slice of the collection starting at the given index:<br />

.<br />

1 $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);<br />

2<br />

3 $slice = $collection->slice(4);<br />

4<br />

5 $slice->all();<br />

6<br />

7 // [5, 6, 7, 8, 9, 10]<br />

If you would like to limit the size of the returned slice, pass the desired size as the second argument<br />

to the method:<br />

.<br />

1 $slice = $collection->slice(4, 2);<br />

2<br />

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

4<br />

5 // [5, 6]<br />

The returned slice will have new, numerically indexed keys. If you wish to preserve the original<br />

keys, pass true as the third argument to the method.<br />

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

The sort method sorts the collection:

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

Saved successfully!

Ooh no, something went wrong!