06.10.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 288<br />

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

2<br />

3 // 3<br />

If the collection contains nested arrays or objects, you should pass a key to use for determining<br />

which values to calculate the average:<br />

1 $collection = collect([<br />

2 ['name' => 'JavaScript: The Good Parts', 'pages' => 176],<br />

3 ['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096],<br />

4 ]);<br />

5<br />

6 $collection->avg('pages');<br />

7<br />

8 // 636<br />

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

The chunk method breaks the collection into multiple, smaller collections of a given size:<br />

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

2<br />

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

4<br />

5 $chunks->toArray();<br />

6<br />

7 // [[1, 2, 3, 4], [5, 6, 7]]<br />

This method is especially useful in views when working with a grid system such as Bootstrap¹⁶⁶.<br />

Imagine you have a collection of Eloquent models you want to display in a grid:<br />

¹⁶⁶http://getbootstrap.com/css/#grid

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

Saved successfully!

Ooh no, something went wrong!