foreach ($events_id as $id) {
$vis = $visitors->where('event_id', $id);
array_push($men, $vis->where('sex', 'male')->count());
array_push($women, $vis->where('sex', 'female')->count());
array_push($kids, $vis->where('sex', 'kids')->count());
}
I have a collection of visitors and events IDs I want to check how many men – women – kids are in each event
it works but I want something faster
5
Answers
the answer is to use a query like this to get the count of men women and kids in each event
In order to have greater performance it would be better to have integers instead of string to define the gender.
Also if you can modify your database query, it would be interesting to get these data directly through your query.
You can try like this :
Your query can now look like this:
This can be done direct by your database query but I don`t know why you don’t follow it.
But In this case we can only make fast by (if-else or swatch) condition.
IF-ELSE condition:
SWATCH condition:
You can group the result by sex, and then count how many rows are in each group.
OK TRY THIS:
It will work IA.