Laravel 5 Collections: Retrieving the Distinct Values of a Collection Using Strict Comparison Operators With uniqueStrict

April 22, 2018 —John Koster

The uniqueStrict method can be used to all of the unique items in the collection; it behaves similarly to the unique collection method in that it accepts an optional $key argument which can be used to restrain the conditional check when determining uniqueness.

However, unlike the unique method, the uniqueStrict method uses strict comparisons only.


1public function uniqueStrict(
2 $key = null

#Example Use

The following code examples are equivalent:

1use Illuminate\Support\Collection;
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']
11// Get all unique items using strict comparisons.
12$uniqueItems = $collection->unique('name', true);
14// Get all unique items using strict comparisons.
15$uniqueItems = $collection->uniqueStrict('name');

Some absolutely amazing

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.