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 200<br />

.<br />

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

2<br />

3 $filtered = $collection->filter(function ($item) {<br />

4 return $item > 2;<br />

5 });<br />

6<br />

7 $filtered->all();<br />

8<br />

9 // [3, 4]<br />

For the inverse of filter, see the reject method.<br />

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

The first method returns the first element in the collection that passes a given truth test:<br />

.<br />

1 collect([1, 2, 3, 4])->first(function ($key, $value) {<br />

2 return $value > 2;<br />

3 });<br />

4<br />

5 // 3<br />

You may also call the first method with no arguments to get the first element in the collection. If<br />

the collection is empty, null is returned:<br />

.<br />

1 collect([1, 2, 3, 4])->first();<br />

2<br />

3 // 1<br />

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

The flatten method flattens a multi-dimensional collection into a single dimension:<br />

.

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

Saved successfully!

Ooh no, something went wrong!