Skip to content

eloquent count() does not work well with groupBy() #22883

Closed
@fico7489

Description

@fico7489

Eloquent count() completely ignores what is in select and just adds count(*) as aggregate into select part. If get() method generates this query :

select sellers.* from sellers
        left join locations
        on locations.seller_id = sellers.id
        group by sellers.id

count() would generate this :

select count(*) as aggregate from sellers
        left join locations
        on locations.seller_id = sellers.id
        group by sellers.id

but what should really generate is this :

SELECT count(*) FROM (
    select sellers.* from sellers
        left join locations
        on locations.seller_id = sellers.id
        group by sellers.id) as aggregate ;

If we have 2 sellers the :
The first query generated from count() call returns result 1, but there are 2 sellers.
The second query returns 2 as it should.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions