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

Note: toArray also converts all of its nested objects to an array. If you want to get the<br />

underlying array as is, use the all method instead.<br />

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

The toJson method converts the collection into JSON:<br />

.<br />

1 $collection = collect(['name' => 'Desk', 'price' => 200]);<br />

2<br />

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

4<br />

5 // '{"name":"Desk","price":200}'<br />

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

The transform method iterates over the collection and calls the given callback with each item in the<br />

collection. The items in the collection will be replaced by the values returned by the callback:<br />

.<br />

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

2<br />

3 $collection->transform(function ($item, $key) {<br />

4 return $item * 2;<br />

5 });<br />

6<br />

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

8<br />

9 // [2, 4, 6, 8, 10]<br />

Note: Unlike most other collection methods, transform modifies the collection itself. If you<br />

wish to create a new collection instead, use the map method.<br />

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

The unique method returns all of the unique items in the collection:

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

Saved successfully!

Ooh no, something went wrong!