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

5 // default-value<br />

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

The groupBy method groups the collection’s items by a given key:<br />

1 $collection = collect([<br />

2 ['account_id' => 'account-x10', 'product' => 'Chair'],<br />

3 ['account_id' => 'account-x10', 'product' => 'Bookcase'],<br />

4 ['account_id' => 'account-x11', 'product' => 'Desk'],<br />

5 ]);<br />

6<br />

7 $grouped = $collection->groupBy('account_id');<br />

8<br />

9 $grouped->toArray();<br />

10<br />

11 /*<br />

12 [<br />

13 'account-x10' => [<br />

14 ['account_id' => 'account-x10', 'product' => 'Chair'],<br />

15 ['account_id' => 'account-x10', 'product' => 'Bookcase'],<br />

16 ],<br />

17 'account-x11' => [<br />

18 ['account_id' => 'account-x11', 'product' => 'Desk'],<br />

19 ],<br />

20 ]<br />

21 */<br />

In addition to passing a string key, you may also pass a callback. The callback should return the<br />

value you wish to key the group by:

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

Saved successfully!

Ooh no, something went wrong!