How to remove duplicates in collection?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Remove Duplicates from Collections in Laravel Applications Introduction: Working with collections is a common practice in modern programming languages, including PHP's Laravel framework. One of the challenges developers face while working with these collections is how to remove or filter duplicate data elements. In this blog post, we will discuss different approaches and methods to achieve this goal effectively using Laravel. 1. The Simple Method: Array Unique() Function - Laravel provides a useful function called "unique" that can be applied directly on arrays. This method easily removes all duplicate values from the specified array. For example, suppose you have two duplicate entries in your collection:
$items = ['Apple', 'Banana', 'Orange', 'Grapes', 'Orange'];
To eliminate such duplicates, we can use the unique() method from Laravel's Collection class like this:
use Illuminate\Support\Collection;

$uniqueItems = $items->unique();
2. The More Specific Method: Grouping by Keys and Flattening - Another approach involves grouping the collection items by their specific key, which is usually the id in this case, before flattening it. This would help ensure that only one unique entry for each id remains while keeping all other data intact. You can follow these steps: 1. Create a new Collection that will contain your original one as its elements. 2. Iterate through the array and group the items based on their ids, using the "groupBy" method. 3. Flatten the collection to get the desired result with unique id values.
use Illuminate\Support\Collection;

$announcements = $this->categories_ann->map(function ($c) {
    return $c->announcements->map(function ($a) {
        $a->subsribed = true;

        return $a;
    });
});

$flattenedUniqueAnnouncements = $announcements->groupBy("id")->flatten();
3. The Advanced Method: Using Laravel's Unique() Function with a Closure - This approach uses the "unique" method, which was previously introduced but with a custom closure to further control how duplicates are handled. Here's an example where we want to keep only one announcement per category and prioritize the most recent ones:
use Illuminate\Support\Collection;

$announcements = $this->categories_ann->map(function ($c) {
    return $c->announcements->map(function ($a) {
        $a->subsribed = true;

        return $a;
    });
});

$flattenedUniqueAnnouncements = $announcements->uniqueUsing(function ($item1, $item2) {
    return $item1['category_id'] === $item2['category_id'] ? $item1->updated_at > $item2->updated_at : true;
});
This code snippet ensures that each category's announcements are unique and prioritizes the most recent ones. Conclusion: Laravel provides several methods to handle duplicate data in collections, depending on your specific requirements. The "unique" function is a simple yet powerful tool, while grouping and flattening provide more flexibility. Always consider the nature of your application and select the appropriate method to optimize performance and data integrity. By following best practices, you can streamline your Laravel application development process even further.