Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AverageJoe's avatar

Laravel / Eloquent / Counting several entries at once in the same query

I was wondering if there was a way to perform count several times inside the same query for multiple rows with different conditions? For example, I'd like to count the number of entries which are larger than the set values for certain IDs. Let's say that I have this array:

$data= [
    0 => [
        "id" => 1,
        "points" => 25.33
    ],
    1 => [
        "id" => 55,
        "points" => 83.11
    ],
    2 => [
        "id" => 854,
        "points" => 6.05
    ]
]

I'd like to make a query which counts the number of entries in the DB where those entries have MORE points than the IDs I'm using in the array above. I can do multiple queries easily, but I was wondering if there was a way to pack it all into a single query which would give me the same result?

0 likes
4 replies
Sinnbeck's avatar

Uhh.. Did you copy this from somewhere?

This question does not show any research effort; it is unclear or not useful

Please or to participate in this conversation.