187

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?

18 Answers 18

361

This is working for me:

$user_info = DB::table('usermetas')
                 ->select('browser', DB::raw('count(*) as total'))
                 ->groupBy('browser')
                 ->get();
9
  • 1
    Superb! Just added 'browser' to the select thus: select('browser', ...) and got everything needed. You're good, you! youtube.com/watch?v=ravi4YtUTxo
    – kJamesy
    Aug 30, 2013 at 13:35
  • 1
    Thanks. But why doesn't it work when used with Models like User::select('country', DB::raw('count(*) as total')->otherMethods() ? Sep 4, 2015 at 5:06
  • 1
    +v. use \DB instead of DB at controllers
    – Amit Bera
    Jan 5, 2016 at 15:41
  • @AmitBera can you explain the reason?, please
    – JCarlosR
    Jul 22, 2016 at 2:46
  • 18
    Is there any particular reason why you prefer DB::table('usermetas')->.. over Usermeta::.. ?
    – Adam
    Oct 16, 2017 at 8:32
54

This works for me (Laravel 5.1):

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

Thanks Antonio,

I've just added the lists command at the end so it will only return one array with key and count:

Laravel 4

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

Laravel 5.1

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

Laravel 5.2+

$user_info = DB::table('usermetas')
    ->select('browser', DB::raw('count(*) as total'))
    ->groupBy('browser')
    ->pluck('total','browser');
4
  • 2
    Thanks. One note: ->all() in the 5.1 example should be removed, since you already lists the results.
    – Pim
    Feb 27, 2017 at 14:04
  • 1
    list() is deprecated and renamed to pluck() laravel.com/docs/5.2/upgrade#upgrade-5.2.0
    – Arun Code
    Nov 25, 2017 at 5:14
  • how to sort with total after pluck and take 10 high count from this Apr 17, 2021 at 10:51
  • i founded tnx usermetas::groupBy('browser') ->selectRaw('count(*) as total, browser') ->orderBy('total','DESC') ->skip(0)->take($number)->pluck('browser'); Apr 17, 2021 at 11:16
37

If you want to get collection, groupBy and count:

$collection = ModelName::groupBy('group_id')
->selectRaw('count(*) as total, group_id')
->get();

Cheers!

13
  1. Open config/database.php
  2. Find strict key inside mysql connection settings
  3. Set the value to false
2
  • the perfect way to avoid the raw query !! Jan 7, 2021 at 11:15
  • 5
    The perfect way to mess your future sql codes up! And not be able to run it on a standard database without modification. Sep 14, 2021 at 7:13
10

Works that way as well, a bit more tidy. getQuery() just returns the underlying builder, which already contains the table reference.

$browser_total_raw = DB::raw('count(*) as total');
$user_info = Usermeta::getQuery()
    ->select('browser', $browser_total_raw)
    ->groupBy('browser')
    ->pluck('total','browser');
0
5

Laravel Version 8

Removed the dependency of DB

     $counts = Model::whereIn('agent_id', $agents)
        ->orderBy('total', 'asc')
        ->selectRaw('agent_id, count(*) as total')
        ->groupBy('agent_id')
        ->pluck('total','agent_id')->all();
4

Try with this

->groupBy('state_id','locality')
  ->havingRaw('count > 1 ')
  ->having('items.name','LIKE',"%$keyword%")
  ->orHavingRaw('brand LIKE ?',array("%$keyword%"))
2
  • 3
    While this may answer the question, it is better to explain the essential parts of the answer and possibly what was the problem with OPs code.
    – pirho
    Dec 7, 2017 at 11:35
  • Thanks! The havingRaw bit is really nice since it lets you also lets you filter out the results you don't want based on the count. Comes in handy when you want to do some "and-filtering" and have joins for each filter. Nov 19, 2020 at 17:23
4
$post = Post::select(DB::raw('count(*) as user_count, category_id'))
              ->groupBy('category_id')
              ->get();

This is an example which results count of post by category.

4

Since this is the top result when i search for eloquent count with groupby returns only first

using "illuminate/database": "^9.38" in composer. so "should" be the latest at the time of this post

I honestly have no idea why they think that returning the first record is the right option.. ie

IMHO the current implementation doesn't make sense for queries including groupBy statements. Why should be N_1 be the "right" result?

Since @taylorotwell rightly pointed out some performance issues with counting the subquery results, why don't we fix that on the php side, by checking if there are any group statements, and if so, performing a N_1 + N_2 + .... + N_M ?

https://github.com/laravel/ideas/issues/1693#issuecomment-621167890


Wrapping the query and doing a count seems to work for me

$records = ...

$record_count = DB::table( "fake" );
$record_count->fromSub($records->select(DB::raw(1)),"query");
$record_count->count();
  • create a "fake" query builder
  • add a sub "from" from (...) query set the "sub query" to select 1 instead of returning huge column data. not sure if this is needed but in my mind it seems like a good idea
  • do the normal ->count()

returns the "expected" result since it executes:

select count(*) as aggregate from (select 1 from ... group by ...) as `query`

The sub is the "query" that $records would normaly execute


My use-case is for pagination (not using laravel). so i get the record count then pass it to the paginator then call ->forPage()->get()

https://github.com/laravel/framework/issues/44081#issuecomment-1301816710

2
  • I've been searching for hours only to find that its a known issue. Thank you so much mate Dec 18, 2022 at 16:12
  • For someone supporting a version lower than 5.8 you need do it like this (working on my 5.5). since fromSub isn't available for versions before 5.8 . Dec 18, 2022 at 16:32
2

Another way would be this:


$data = Usermeta::orderBy('browser')->selectRaw('browser, count(*) as total')->get()

1

Laravel Eloquent query that uses the GROUP BY clause with advanced aggregation functions and conditional statements.

GroupBy on one column.

$data = Employee::select(
  'department', 
  DB::raw('SUM(salary) as total_salary'), 
  DB::raw('COUNT(*) as total_employees'), 
  DB::raw('SUM(IF(bonus > 13000, 1, 0)) as employees_with_bonus')
)
->groupBy('department')
->havingRaw('total_employees > 5 AND total_salary > 10000')
->orHavingRaw('department_rank LIKE ?', array("%$keyword%"))
->get();

In the above query, if the bonus is greater than 13000, the IF function returns 1 otherwise it returns 0.

GroupBy on two columns: The groupBy method takes multiple arguments

$data = Employee::select(
  'department', 
  'location', 
  DB::raw('SUM(salary) as total_salary'), 
  DB::raw('COUNT(*) as total_employees'), 
  DB::raw('SUM(IF(bonus > 1000, 1, 0)) as employees_with_bonus')
)
->groupBy('department', 'location')
->havingRaw('total_employees > 5 AND total_salary > 10000')
->get();

GroupBy and JOIN: Laravel Eloquent query that joins two tables and uses grouping and aggregate functions. (inner join)

$data = Employee::select(
  'employees.department', 
  'employees.location', 
  DB::raw('SUM(employees.salary) as total_salary'), 
  DB::raw('COUNT(*) as total_employees'), 
  DB::raw('SUM(IF(employees.bonus > 1000, 1, 0)) as employees_with_bonus')
)
->join('departments', 'employees.department', '=', 'departments.name')
->groupBy('employees.department', 'employees.location')
->havingRaw('total_employees > 5 AND total_salary > 10000')
->get();

Raw MySQL Query:

SELECT 
  department, 
  SUM(salary) AS total_salary, 
  COUNT(*) AS total_employees, 
  SUM(IF(bonus > 1000, 1, 0)) AS employees_with_bonus 
FROM 
  employees 
GROUP BY 
  department 
HAVING 
  total_employees > 5 AND total_salary > 13000;
0

If you want to get sorted data use this also

$category_id = Post::orderBy('count', 'desc')
    ->select(DB::raw('category_id,count(*) as count'))
    ->groupBy('category_id')
    ->get();
0

In Laravel 8 you can use countBy() to get the total count of a group.

Check the documentation on the same. https://laravel.com/docs/8.x/collections#method-countBy

1
  • 5
    This is for Collections and not for Eloquent models. It would be inefficient to get all of the records from the database and then perform a groupby.
    – Flipper
    Feb 28, 2022 at 20:31
0

Simple solution(tested with Laravel 9 and Spatie/Permissions).

Controller:

//Get permissions group by guard name(3 in my case: web, admin and api)
$permissions = Permission::get()->groupBy('guard_name');

View:

@foreach($permissions as $guard => $perm)
  <div class="form-group">
    <label for="permission">Permissions ({{ ucfirst($guard) }}) {{ count($perm) }}</label>
    <select name="permission[]" id="permission" class="form-control @error('permission') is-invalid @enderror" multiple>
      @foreach($perm as $value)
        <option value="{{ $value->id }}">{{ $value->name }}</option>
      @endforeach
    </select>
    @error('permission')
      <div class="invalid-feedback">
        {{ $message }}
      </div>
    @enderror
  </div>
@endforeach
0

It prints all columns data. It works for me:

$user_info = DB::table('usermetas as u1')
    ->leftJoin('usermetas as u2', function ($join) {
        $join->on('u1.browser', '=', 'u2.browser')
             ->whereRaw('u1.created_at < u2.created_at');
    })
    ->where('u2.id', '=', null)
    ->select('u1.*')
    ->orderBy('u1.id')
    ->get();
0

#laravel9 :

User::select('city','count, count(*) as total')->groupBy('city')->get();
-2

Here is a more Laravel way to handle group by without the need to use raw statements.

$sources = $sources->where('age','>', 31)->groupBy('age');

$output = null;
foreach($sources as $key => $source) {
    foreach($source as $item) {
        //get each item in the group
    }
    $output[$key] = $source->count();
}
2
  • 13
    This is memory and processing hungry. Jun 4, 2018 at 3:46
  • Same memory pb for me
    – Vince
    Jun 15, 2020 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.