$this->collection, ]; } public function with(Request $request): array { $persons = $this->collection; $byCrowdType = $persons->groupBy(fn ($person) => $person->crowd_type_id); $crowdTypeMeta = []; foreach ($byCrowdType as $crowdTypeId => $group) { $crowdTypeMeta[$crowdTypeId] = [ 'approved_count' => $group->where('status', 'approved')->count(), 'pending_count' => $group->where('status', 'pending')->count(), ]; } return [ 'meta' => [ 'total' => $persons->count(), 'approved_count' => $persons->where('status', 'approved')->count(), 'pending_count' => $persons->where('status', 'pending')->count(), 'by_crowd_type' => $crowdTypeMeta, ], ]; } }