Laravel, being empty array in PHP

Laravel Collections: PHP Arrays On Steroids

Laravel collections are one of the most powerful provisions of the Laravel framework. They are what PHP arrays should be, but better.

Laravel collections are what PHP arrays should be, but better.

In this tutorial, we will be going through a few tricks that may be handy when you are working with collections.

The Collection Class

The class provides a convenient wrapper for working with arrays.

The class implements some PHP and Laravel interfaces such as: -

You can check out the rest of the implemented interfaces here.

Creating A New Collection

A collection can be created from an array using the helper method or by instantiating the class.

A really simple example using the helper method:

And a more hashed out one:

BeginnerTailwind.comLearn Tailwind CSS from Scratch

The helper method is much easier easier to work with since you do not need to instantiate the class.

I also used the helper method to display the collection on the browser. This should look a little like this.

Eloquent ORM Collections

The Laravel Eloquent ORM also returns data as collections.

Eloquent ORM calls return data as collections

To demonstrate such feedback, I will set up an sqlite database.

We will then create a users table with the default migrations that come with Laravel and seed 10 records into the users table.

The controller method below returns a laravel collection with a list of all users as shown below.

You can then simply access a collecton attribute using the arrow notation. For instance, to get the first user's name from the collection, we can have the following.

Creating Our Sample Collection

We will go through some of the most useful collections tricks that you may find handy.

For the next few sections, I will use the following set of data from the users table and some custom collections for demonstration purposes. While we're creating information here manually, we could also use Laravel's model factory

Finding data

There are a number of ways to find data in a collection.

The method takes a single value, a key-value pair of parameters or a callback function and returns a boolean value of the value is present in the collection or not.

You can use the where method to search a collection by a given key, pair value.

You can also chain your methods. How cool is that?

There are a few more where-like methods that I will not delve into but you can check them out on the Laravel documentation.

Most notably, you can take a look at:

  • whereIn () - Takes a key value pair to search but accepts an array of values ​​to search.

  • search () - Searches for a value in a collection, returns the index if the value is present, and if it is not.

  • has () - returns a boolean value if a key value is present in a collection or not.

Filtering data

You have probably guessed it by now that filtering in collections is done using a method.

You also have figured it out on your own that the filter method takes a callback function that subjects our collection to a filter test, right? Right?

The filter method takes a key and a value as parameters in the callback function. If your condition tests true, the value is added into the assigned retained.

I have also introduced a method which returns the underlying array from a collection. Awesome, right? Right?

Sorting / ordering data

Collections enable us to sort data using two simple methods: -

  • - Sort data in ascending order given a criteria
  • - Sort data in descending order given a criteria

The sort methods takes a key or callback function parameter which is used to sort a collection.

The sort methods maintain the keys for each value. While this may be important for your application, you can reset them to the default zero based incremental values ​​by chaining the method.

As usual, I also have thrown in a new collection method which simply converts a collection to an array.

Grouping data


Grouping a collection helps makes sense of your data. The groupBy method takes either a key or callback function and returns a grouped collection based on on the key value or the returned callback value.

Getting A Subset Of Data

Given an array of data, and subsequently a collection, you may want to get a section of the it. This could be:

  • The first 2 records
  • The last 2 records
  • All the records but in groups of 2.

Collections bless us with a few methods that do just that.

The take method takes an integer value and returns the specified number of items. Given a negative number, returns the specified number of items from the end of the collection.

The chunk method breaks a collection into smaller collections of the size n.

There are quite a number of ways in which this may come in handy.

When you pass the data to a blade view, you can chunk it to get n rows at a time, say, to fit every 3 names into row.

You can also do the reverse of by combining smaller collections into a larger one using the method. Check it out here.

Iterating through data

The map function iterates through a collection and subjects each value to a callback function.

We will create a collection of peoples names and return a collection of the length of each of the names.

While the map method creates a new collection, sometimes you would want to edit the original collection. The transform method takes a callback method and performs an action on the same collection.

Since transforming does not create a new collection, you do not need to assign it to a new value.

Unlike the map and transform methods, the reduce method returns a single value. It passes the result of each iteration to the next iteration.

For instance, to get the sum of integer values ​​in a collection, reduce passes the sum of subsequent numbers and performs an addition of the result to the next number iteratively.

The each method passes each of the items through a callback function.

The most interesting section about the each method is that you can easily break out of the iteration by simply returning false in the callback function.

The every method creates a new collection that is made up of every n-th element in a collection.

Using A Collection As A Set

The collection class also provides methods that help us work with data as sets. This means we can compare two data sets and perform actions based on that.

The union () method is used to add values ​​to a collection from an array. If a value already exists in the collection, the value in the array is ignored.

The intersect () method removes the elements in a collection that are not in a passed array or collection.

You will notice that the intersect method preserves the keys for the returned values.


I have tried to cover most of the collection methods that you might find yourself using but there is still much left out there to learn.

Most notably, I left out the following

There is still much more on the Laravel documentation and the Laravel API documentation that you can do with collections that you may want to take a look at.

To follow up on this tutorials code, check out the github repo here. Feel free to make your contributions on it.

Like this article? Follow @_kar_is on Twitter