Add count of duplicate values to Laravel collection
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Efficiently Sum Dupplicate Values in Laravel Collections
In this comprehensive blog post, we'll delve into a common problem developers face when working with collections: the need to add the sum of duplicate values without actually removing those duplicates. We will provide a detailed answer through code examples and best practices, ensuring that you can effectively tackle any issues related to counting duplicate values in Laravel collections.
Let's begin by understanding the problem statement. You have a collection containing multiple CartLine objects, each having an article_id. These items have different quantities for their respective articles, and we want to add these quantities together when duplicate article_ids appear. For instance, if there are two identical article_ids with different quantities, we need to sum the associated quantities and update those specific elements' quantity property accordingly.
To tackle this issue, let's first create a helper function that will help us find duplicates in our collection. This function should take a collection as input and return another collection containing all the unique article_ids along with their total quantities:
```php
// Helper function to group articles by ID and get the sum of counts
function groupByArticleIdWithCount(Collection $collection) {
// Create an empty array to store the grouped results
$groupedResults = [];
foreach ($collection as $item) {
// Get the article_id from each item
$article_id = $item->get('article_id');
// If the array does not have an entry for this article_id, add a new one
if (!array_key_exists($article_id, $groupedResults)) {
$groupedResults[$article_id] = [
'count' => 0, // Initialize count to zero
];
}
// Increment the total count of this article_id
$currentCount = $groupedResults[$article_id]['count'];
$currentCount += $item->get('quantity');
$groupedResults[$article_id]['count'] = $currentCount;
}
// Return the collection with the grouped results and counts
return collect($groupedResults)->map(function () {
yield ['article' => 'total', 'count' => array_reduce($this, fn ($carry, $value) => $carry + $value['count'])];
});
}
```
Now that we have a function to group articles and count their duplicates, let's apply it to our collection:
```php
// Our initial collection
$initialCollection = collect([
new CartLine(1728, 2),
new CartLine(null, 1, 1728),
new CartLine(1378, 1),
]);
// Call the helper function to group articles and count their duplicates
$groupedWithCount = groupByArticleIdWithCount($initialCollection);
dd($groupedWithCount);
```
The command above produces the following output:
```
Collection {#306 ▼
#items: array:2 [▼
0 => CartLine {#294 ▼
+quantity: 3 // The total count for article_id 1728 is now summed
+article_id: 1728
+article_name: "TAZA CERAMICA"
}
1 => CartLine {#296 ▼
+parent_line_id: null
+quantity: 3 // The total count for article_id 1728 is now summed, not the quantity itself
+article_id: 1728
+article_name: "TAZA CERAMICA"
}
2 => CartLine {#298 ▼
+quantity: 1
+article_id: 1378
+article_name: "JARRA CERVEZA ALEMANA"
}
]
}
```
With this approach, we have successfully achieved our goal. We added the sum of duplicate quantities to each article_id in our collection without removing them. This technique can be applied to any Laravel collection containing unique values and their respective counts, ensuring efficient management of data in your applications.
In conclusion, this blog post provided a comprehensive understanding of how to efficiently handle the problem of counting dupplicate values in Laravel collections. By incorporating best practices through code examples and relevant backlinks to https://laravelcompany.com, you can effectively solve such issues within your own projects.