Search

Laravel 5 Collections: Retrieving the Distinct Values of a Collection With unique

April 22, 2018 —John Koster

The unique method can be used to get all the unique items in the collection. It accepts an optional $key argument which can be used to further restrict which items are returned. The unique method returns a new instance of Collection with the items it determined to be unique.

#Signature

1public function unique(
2 $key = null,
3 $strict = false
4);

#Example Use

The following code example shows the basic usage of the unique method:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 'a', 'A', 'b', 'c', 1, 1, 2, 3
6]);
7 
8// Get all unique items.
9$uniqueItems = $collection->unique();

After the above code executes, the $uniqueItems variable will be an instance of Collection and will have a value similar to the following output. It should be noted that the unique method is case sensitive.

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => string 'a'
5 1 => string 'A'
6 2 => string 'b'
7 3 => string 'c'
8 4 => int 1
9 6 => int 2
10 7 => int 3

The following code sample demonstrates how to use the unique method to get all the unique items in a collection based on a given property of the collection's items (in this example it is the name of hypothetical users):

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 ['id' => 5, 'name' => 'Jane', 'sex' => 'female'],
6 ['id' => 6, 'name' => 'Bill', 'sex' => 'male'],
7 ['id' => 7, 'name' => 'Sarah', 'sex' => 'female'],
8 ['id' => 8, 'name' => 'Sarah', 'sex' => 'female']
9]);
10 
11// Get all unique items.
12$uniqueItems = $collection->unique('name');

After the above code has finished executing the $uniqueItems variable will be an instance of Collection and will have a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 =>
5 array
6 'id' => int 5
7 'name' => string 'Jane'
8 'sex' => string 'female'
9 1 =>
10 array
11 'id' => int 6
12 'name' => string 'Bill'
13 'sex' => string 'male'
14 2 =>
15 array
16 'id' => int 7
17 'name' => string 'Sarah'
18 'sex' => string 'female'

The provided $key can also be a callback function that accepts one argument. The argument will be the item from the collection. The callback function should return the value the unique method uses to determine uniqueness. The following example is similar to the first example in this section, but will effectively perform a case-insensitive uniqueness check:

1use Illuminate\Support\Collection;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 'a', 'A', 'b', 'c', 1, 1, 2, 3
6]);
7 
8// Get all unique items.
9$uniqueItems = $collection->unique(function($item) {
10 return strtolower($item);
11});

After the above code has been ran, the $uniqueItems variable will be an instance of Collection and contain a value similar to the following output:

1object(Illuminate\Support\Collection)
2 protected 'items' =>
3 array
4 0 => string 'a'
5 2 => string 'b'
6 3 => string 'c'
7 4 => int 1
8 6 => int 2
9 7 => int 3

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.