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

1 $collection = collect([<br />

2 ['account_id' => 1, 'product' => 'Desk'],<br />

3 ['account_id' => 2, 'product' => 'Chair'],<br />

4 ]);<br />

5<br />

6 $collection->implode('product', ', ');<br />

7<br />

8 // Desk, Chair<br />

If the collection contains simple strings or numeric values, simply pass the “glue” as the only<br />

argument to the method:<br />

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

2<br />

3 // '1-2-3-4-5'<br />

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

The intersect method removes any values that are not present in the given array or collection:<br />

1 $collection = collect(['Desk', 'Sofa', 'Chair']);<br />

2<br />

3 $intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']);<br />

4<br />

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

6<br />

7 // [0 => 'Desk', 2 => 'Chair']<br />

As you can see, the resulting collection will preserve the original collection’s keys.<br />

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

The isEmpty method returns true if the collection is empty; otherwise, false is returned:

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

Saved successfully!

Ooh no, something went wrong!