Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.6 chunk with groupBy

I am getting the error while executing below, while without groupBy it is working fine.

    User::select('col')->groupBy('col1')->chunk(100, function ($users) {
        foreach ($users as $user) {
            dump($user);
        }
    });

Error:

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
like image 479
zarpio Avatar asked Oct 20 '25 15:10

zarpio


1 Answers

you can do something like that need to use orderBy

User::select('col')
    ->orderBy('col1')
    ->groupBy('col1')
    ->chunk(100, function ($users) {
        foreach ($users as $user) {
            // code ..
        }
    });
like image 113
bhavinjr Avatar answered Oct 23 '25 06:10

bhavinjr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!