Level 44
Your return from the map is overwriting the value of $item, so you'd have to add to the $item then return it, something like:
SancofaUser::where('created_at', '>=', $days_after_now)
->groupBy('department')
->get()
->keyBy('department')
->map(function($item, $key) {
$item['count'] = count($item);
return $item;
});
However, you'd be better of doing this with query builder rather than looping over the results afterwards, something like:
$data = DB::table("yourtablename")
->select("department", DB::raw("COUNT(*) as count"))
->groupBy("department")
->get()
->keyBy('department');