11.08.2017 Views

codebright

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Eloquent Collections 344<br />

1 $allAlbums = Album::all();<br />

2 $albumOfTheYear = $allAlbums->first();<br />

We retrieve all of our albums as usual, but this time we use the first() method on the collection to<br />

retrieve the album of the year. This results in only a single query.<br />

Choosing whether to perform a query on the collection, or on the database is a matter of choice.<br />

Querying against the database will allow for faster and more complex searches that would result in<br />

high CPU / memory usage on the webserver if performed on the collection. Then again, using the<br />

collection effectively can also benefit your application with fewer individual SQL queries.<br />

Use your best judgement to decide when to use the collection helper methods. I trust you!

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

Saved successfully!

Ooh no, something went wrong!