I have a collection which is like this:
IlluminateSupportCollection {#456 ▼ // appHttpControllersUserController.php:42
#items: array:4 [▼
0 => array:10 [▼
"to" => "12145"
"from" => "1833"
"type" => "MMS"
"smsCount" => 3
"status" => "PENDING"
"error" => "No Error"
"description" => "Message sent, waiting for delivery report"
"sentAt" => "2024-04-06T05:29:20.791Z"
"doneAt" => "2024-04-06T05:29:22.779Z"
"messageId" => "27a13e81"
]
1 => array:10 [▼
"to" => "1215"
"from" => "18334"
"type" => "MMS"
"smsCount" => 3
"status" => "DELIVERED"
"error" => "No Error"
"description" => "Message delivered to handset"
"sentAt" => "2024-04-06T05:26:44.683Z"
"doneAt" => "2024-04-06T05:26:46.423Z"
"messageId" => "ebb9330c"
]
2 => array:10 [▶]
3 => array:10 [▶]
]
#escapeWhenCastingToString: false
}
I want to filter the arrays based on type (type could be MMS & SMS) and month from sendAt. I want to send the data to chart.js. So I want to filter type every months no of MMS & SMS sent.
How can i grouping the collection?
I was trying in following way, But couldn’t get appropriate output?
$selectedElements = new Collection($selectedElements);
$filteredAndGrouped = $selectedElements
->where('type', 'MMS')
->groupBy(function ($item) {
// Extract year and month from the date string
$dateParts = explode('-', substr($item['sentAt'], 0, 10)); // Extract yyyy-mm from the date string
return $dateParts[0] . '-' . $dateParts[1]; // Return yyyy-mm format
});