Last active
September 7, 2022 14:50
-
-
Save manashcse11/6234ed3b7b8a6bb45157cbca25277a7e to your computer and use it in GitHub Desktop.
#Laravel collection #group by with sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$collection = collect([ | |
['item_id' => 10, 'status_id' => 1, 'point' => 3], | |
['item_id' => 11, 'status_id' => 2, 'point' => 2], | |
['item_id' => 12, 'status_id' => 3, 'point' => 4], | |
['item_id' => 13, 'status_id' => 3, 'point' => 1], | |
]); | |
$grouped = $collection->groupBy('status_id') | |
->map(function ($item) { | |
return $item->sum('point'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment