This blog post is an in-depth walk though of a tweet I sent out a few weeks ago. You can probably get everything you need from the tweet and the comments on it, but I’ve written this to make it Google-able if ever needed, and to cover some caveats you should be aware of.
Out of the box Laravel comes with a long list of conveniences and scaffolding that help developers build out web applications quickly and efficiently. One of those conveniences is the co-location of eloquent attributes, relationships, and scopes on the model class. As your application grows, more than likely your models will also grow as you add more attribute mutators, new relationships, additional query scopes, and other functionality. The model is the perfect place for these things initially, but at a point I personally find I want the ability to thin out my models.
The goal here is to thin models by moving query scope methods from the model to a dedicated query builder class, on a per model basis. If your models get larger than you would prefer, I feel that this is the natural place for scopes to be relocated to. Because scopes are an indirect way of extending the eloquent builder on a per class basis, extending the actual query builder on a per class basis just feels right to me.
A couple of ancillary benefits also arise from introducing this pattern, including:
- The ability to click through to method definitions for query scopes (because we are removing the
- Static analysis is able to understand your scopes (because we are removing the magic behind them)
It also does not change the public facing API for how you access / interact with your eloquent queries! But enough prologue, let’s look some code.
The Team Model
We are going to use the following model as a starting point to show how to refactor out query scope methods to a dedicated query builder. In reality I would not apply the refactor to this actual model until it was substantially bigger, but we can all use our imagination to pretend we have a bunch more stuff on this model.
In this example model we have four query scope methods that we are looking to refactor off the model to a dedicated query builder:
The Query Builder
The base eloquent model has a
newEloquentBuilder method that returns the same query builder class for each type of model in your application.
As this method is located on the model, we are able to override it in our application models and return our dedicated query builder. To get started we will create an empty class that extends the base eloquent builder.
Now we have created the dedicated query builder, we will override the
newEloquentBuilder method on our
Team model. Note that you will want to create a builder class for each model you are applying this refactor to.
The great thing about this refactor is that it isn’t all or nothing. We are now using our
TeamBuilder class whenever we interact with query scopes for the
Team model, however all the scopes still located on the model will continue to work as expected. You could leave existing scopes on the model and only add new scopes to the dedicated query builder, if your heart so desires, although I personally wouldn’t recommend it as it might cause confusion when trying to locate the scope methods because they are split across two classes.
I’m going to break this refactor down into the four smallest steps possible to make sure we don’t miss anything along the way.
Step 1: Relocate the scope
I’m going to walk through moving the
scopeWhereRankedHigherThan method from the model to the new builder. First thing is to remove it from the model…
and place it as is on the new builder class…
Step 2: Remove the $builder parameter
When the scope is on the model, Laravel under the hood passes the query builder instance into the scope - but now we are working directly with the query builder, so this won’t happen anymore. But do not fret - we just remove the first
$builder parameter and replace any reference to it in the method body with
Step 3: Remove the “scope” prefix and fix the casing.
In order to tell eloquent that the methed on the model is a query scope and not just another method on the model, we start the method name with
"scope". We can now remove this prefix, but we also need to make sure the method matches the casing used when we call query scope, so we also need to lowercase the first letter in the method name after removing the prefix.
Step 4: Return
The last piece of magic Laravel does for us under the hood is ensure that if we do not return a value from the scope, it will automatically return the query builder instance for us. But now we need to make sure we do that ourselves.
and that is all the steps involved in this refactor. Applying the refactor the the rest of the query scopes we end up with the following builder class…
…and our model is looking much cleaner now as we have relocated all of the query scopes.
Caveat: Multi-where Scopes
If you are chaining multiple
wheres in a single scope then you need to group them manually when using this new approach. Eloquent under the hood detects how many
wheres you applied in a single scope and automatically groups them if you add more than one. Here is an example of how you would manually group multiple
I personally write very small scopes that generally only have one
where method in them. I then combine scopes at a higher level (i.e. when calling them) to get the desired groupings. This gives me more fine-grained control and reuse of my scopes.
Oh! So I can like bind this to the container then!?!
Well, like, you could…but you shouldn’t IMO. Although this approach removes some of the work done under the hood - it doesn’t remove all of it. When the builder is created, eloquent still does some trickery to set the model and do a few other things to the builder before returning it.
Also, binding it to the container sounds like you are going to want to mock it. Mocking the database sounds like a bad time. Just continue to access the query builder like you always have - directly off the model itself. i.e.
I’d recommend wrapping your eloquent scope calls in a repository if you really need to mock something for a specific test.
Another thing is that you are going to have to bind each builder to the container individually. Does not sound nice…but I’ll concede: I am not your Dad and you can do what you want. If for whatever reason you do want to bind it to the container, do it like this in a service provider…
Another topic that came up in the Twitter thread was sharing scopes with traits. If you want to share a handful of scopes with a particular set of models, then traits are still a great way to go about that. You can use the trait on the model as you have been doing, or you could use the dedicated query builder approach and use the trait on the query builders (ensuring that the way you are defining the scope methods matches where you put them).
If, however, you are looking to share scopes across all your models, you can also utilise inheritance. I’ve usually got a handful of general helper scopes that I use across all of my models. To do this I create a base query builder (like the one shown below) and all my model specific query builders inherit from this. Yes, it is another layer of inheritance, but I do the same thing with my models to add a couple of helpers. It works quite well.
If you are into static analysis, you will be happy to know this approach to query scopes works perfectly well without any helper packages or plugins. PHPStan (with as many strict rules as I could find) and Psalm are completely happy! You need to specify a couple of return typehints on your models, and then ensure you start your queries in a specific way, but after that you are ready to roll. For a start, you’ll need to specify a return type on the static
query method on your model.
Now when you want to start a query from the model, you kick things off by calling the static
query() method, which Eloquent usually does under the hood for you.
You will also need to go through and add parameter typehints and return typehints to your query builder methods, but the
Model::query() return typehint is the one that will help make the static analysis dream a reality.
Hopefully you’ve found this approach interesting. I don’t think you should reach for this technique out of the box, but I find it a great way to whip some growing models into shape by splitting out the query scopes to dedicated classes.
You should always consider if this technique is appropriate for you application, your team, and your programming style before implementing the refactor.
If you have any questions or thoughts on any of these ideas I would love to chat - reach out on Twitter anytime.