2

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)

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. But why 1st query doesn't work? Return no errors, just nothing changes (e.g. I have 3 records with author_id = 1 but it returns 3 records instead of 1).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.