Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Handling model data in Laravel collections

Wednesday, August 5th, 2015 by Servage

collectionThere is a fluent collection wrapper provided in Laravel which makes handling many models very efficient. A collection is basically a list of models, like an array, which can be processed using various methods. The collection itself is an object which provides a range of utilities, however, it also implements array access, so you can perform array operations on it. This makes it especially powerful.

It is very easy to get a collection, because you get it by default from the Eloquent model database query methods. For example if you retrieve all users using the standard methods for such a database selection, you will get a collection of user model instances.

Creating a collection from variable values

The following code creates a collection of some array available in the code. It illustrates how collections can be used to handle any kind of data list. Collections could essentially be used whenever you would have otherwise used the less powerful default array implementation in PHP.

// Simple data array
$data = [1, 2, 3];

// Convert the data into a collection object
$collection = collect( $data );

Getting a collection from the database

Below is a collection automatically returned from the user model’s “get” method, containing all the matched user records.

// Return a collection from the database
$models = User::get();

Iterating over collection models

You can iterate over a collection just like any regular array, because the array access interface is implemented in the collection class.

// Iterate over each item in the collection
for ( $collection as $item )
	// Do something with each item

Alternatively you can also use the collection’s “each” method

The “each” methods provides a callback driven implementation of a handler for each element in the collection.

// Iterate over each item in the collection
$collection = $collection->each(function ($item, $key)
  // Do something with each item

Accessing collection models by id

If you wish to access elements in the collection directly, it makes sense to sort the collection by id. Thereafter you can access the models directly.

// Keying items by id value
$items = $collection->keyBy( 'id' );

// Retrieving item by id value
$item = $items[ 'some-id' ];

Getting all elements

Your can retrieve all elements of the collection with the “all” method.

// Get all items as array
$items = $collection->al();

The collection object offers a wide range of other utility functions to ease the work. You may wonder if using the collection has a bad impact on your performance? That depends largely on how large the collections are you manage and how many iteration commands you run. Looping over thousands of elements multiple times can significantly slow your site – but that would be the case with the default array implementation as well. Consider working with less data at a time by using pagination or other techniques to split up your data in more digestible chunks.

Handling model data in Laravel collections, 4.5 out of 5 based on 4 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.