简体   繁体   中英

Laravel groupBy doesn't work properly with PostgreSQL

I have same number of rows with groupBy as well without it.

$authors = AuthorMask::where('company_id', $user->company_id)
        ->groupBy('id', 'author_id')
        ->orderBy('created_at', 'desc')
        ->paginate(5);

If I use this:

$authors = AuthorMask::where('company_id', $user->company_id)
        ->groupBy('author_id')
        ->orderBy('created_at', 'desc')
        ->paginate(5);

I get an error.

SQLSTATE[42803]: Grouping error: 7 ERROR: column "author_masks.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: select * from "author_masks" where "company_id" = $1 group b... ^ (SQL: select * from "author_masks" where "company_id" = 1 group by "author_id" order by "created_at" desc limit 5 offset 0)

Unfortunately, it's impossible to run 2nd query with PostgreSQL, as it requires all fields that are specified in SELECT list to be part of the GROUP BY expression.

you can check over here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM