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

cconey's avatar

Extracting Where Clause from Eloquent

I can't seem to find a good way to pull a full where clause from an instance of the QueryBuilder.

We have a very advanced reporting API, and everything is good except every now and again we need to apply the same filtering parameters on the full set of data, instead of the limited set (we are paginating). The only way that I can think to do that would be to somehow extract the where clauses and apply them to another query.

Any ideas how to do this cleanly?

EDIT:

Let me see if I can demonstrate more clearly.

So we have a list of transactions that we allowing filtering, grouping, ordering, limiting, and paginating, which works fine. You get a response like this in a lot of cases:

{

    "status_code": 200,
    "message": "OK",
    "data": {
        "sales": [
            {
                "id": 1,
                "name": "Type 1",
                "value": 100.07
            },
            {
                "id": 2,
                "name": "Type 2",
                "value": 1200.10
            },
            {
                "id": 3,
                "name": "Type 3",
                "value": 89656.15
            },
            {
                "id": 4,
                "name": "Type 4",
                "value": 0
            }
        ]
    },
    "meta": {
        "pagination": {
            "total_items": 4,
            "items_per_page": 15,
            "current_page": 1,
            "total_pages": 1
        }
    }

}

So - I got a list of groups of transaction types and the values associated - awesome. but on top of that I want to get a grand total for all of the groups. I could do that easily in php for this specific case, but not as a general use case because of the pagination. The only other way would be to do a separate query using same filtering parameters (without the limit) or to do a sub-query, which I really don't want to do.

Hopefully that demonstrates a little better what I am trying to do.

0 likes
1 reply
thepsion5's avatar

It seems like Query Scopes would be one way to accomplish what you need.

For example:

class Avenger extends Eloquent
{
    public function scopeCanFly($query)
    {
        $query->where('power_armor_sets_owned', '>', 0)
            ->orWhere('jetpacks_owned', '>', 0);
    }

    public function scopeAngrierThan($query, $minAngerLevel)
    {
        $query->where('anger_level', '>', $minAngerLevel);
    }
}

And call it like so:

Avenger::canFly()->angierThan(5)->get();

EDIT:

After seeing your example, I'd probably use a repository class with a chainable filtering method, similar to a post I made earlier today here: https://laracasts.com/discuss/channels/general-discussion/how-to-create-a-laravel-restful-api-allowing-requests-with-url-params/replies/20919

Using that code, you could have a "count" method on your repository that would apply the filters and return the count, and then an "all" method that would apply the filters and return the paginated result:

$total = $avengersRepo->total();
$all = $avengersRepo->all();

Please or to participate in this conversation.