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

.<br />

1 collect([])->isEmpty();<br />

2<br />

3 // true<br />

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

Keys the collection by the given key:<br />

.<br />

1 $collection = collect([<br />

2 ['product_id' => 'prod-100', 'name' => 'desk'],<br />

3 ['product_id' => 'prod-200', 'name' => 'chair'],<br />

4 ]);<br />

5<br />

6 $keyed = $collection->keyBy('product_id');<br />

7<br />

8 $keyed->all();<br />

9<br />

10 /*<br />

11 [<br />

12 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],<br />

13 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],<br />

14 ]<br />

15 */<br />

If multiple items have the same key, only the last one will appear in the new collection.<br />

You may also pass your own callback, which should return the value to key the collection by:<br />

.<br />

1 $keyed = $collection->keyBy(function ($item) {<br />

2 return strtoupper($item['product_id']);<br />

3 });<br />

4<br />

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

6<br />

7 /*<br />

8 [<br />

9 'PROD-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],<br />

10 'PROD-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],<br />

11 ]

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

Saved successfully!

Ooh no, something went wrong!