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

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

The forPage method returns a new collection containing the items that would be present on a given<br />

page number:<br />

.<br />

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

2<br />

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

4<br />

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

The method requires the page number and the number of items to show per page, respectively.<br />

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

The get method returns the item at a given key. If the key does not exist, null is returned:<br />

.<br />

1 $collection = collect(['name' => 'taylor', 'framework' => '<strong>laravel</strong>']);<br />

2<br />

3 $value = $collection->get('name');<br />

4<br />

5 // taylor<br />

You may optionally pass a default value as the second argument:<br />

.<br />

1 $collection = collect(['name' => 'taylor', 'framework' => '<strong>laravel</strong>']);<br />

2<br />

3 $value = $collection->get('foo', 'default-value');<br />

4<br />

5 // default-value<br />

You may even pass a callback as the default value. The result of the callback will be returned if the<br />

specified key does not exist:<br />

.

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

Saved successfully!

Ooh no, something went wrong!