Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

I would have the same approach.
To select the count from something grouped, you can use a Raw exression. The group by clause is simple.

$ethnicityAndCountArray = Employee::select(DB::Raw('Ethnicity, COUNT(*) as count'))
->groupBy('Ethnicity');

The result will be an array with objects containing Ethnicity and count each.

$total = 0;
foreach($ethnicityAndCountArray as $ethnicity)
$total += $ethnicity->count;
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

tpbsv77 tpbsv77 Joined 12 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.