7 months ago

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

Posted 7 months ago by michapietsch


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?

Please sign in or create an account to participate in this conversation.