Search code examples
laravellaravel-4eloquent

Laravel Eloquent groupBy() AND also return count of each group


I have a table that contains, amongst other columns, a column of browser versions. And I simply want to know from the record-set, how many of each type of browser there are. So, I need to end up with something like this: Total Records: 10; Internet Explorer 8: 2; Chrome 25: 4; Firefox 20: 4. (All adding up to 10)

Here's my two pence:

$user_info = Usermeta::groupBy('browser')->get();

Of course that just contains the 3 browsers and not the number of each. How can I do this?


Solution

  • This is working for me:

    $user_info = DB::table('usermetas')
                     ->select('browser', DB::raw('count(*) as total'))
                     ->groupBy('browser')
                     ->get();