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

michapietsch's avatar

Local query scopes vs. Collection macros --- shaping complex data

Hi,

I already defined scopes like scopePublished, scopeActive and so on.

Then I needed to return some complex statistics and I decided to first fetch all the data and then shape it with collection methods to determine e.g. "How many published per group and period", "How many not" and so on.

So I already had filter logic defined in scopes. But query scopes need queries, of course. And I don't want recursion and n+1 queries.

Now this made me wonder: Why not use collection macros to define the filters in the first place, so that they are available independent from Eloquent queries?

Although at the moment I don't know of a way to make collection macros model-specific, but indeed these filters are specific to the given model.

What approach would you take in this scenario?

0 likes
2 replies

Please or to participate in this conversation.