In this episode, we kick things off by building a custom query builder for our User
model. Instead of using Laravel's typical scope methods (like scopeHasVerifiedEmail
), we move this logic into a dedicated builder class. The good news? The way you actually call these methods in your code remains exactly the same—nobody using your models will even notice the difference.
We go through the process step-by-step: overriding the newEloquentBuilder
method in the User model, creating a dedicated UserBuilder
class, and moving the scope logic into regular methods there. This results in a cleaner and more organized codebase, with all your user-related query logic grouped together, rather than cluttering up the model itself.
Finally, we test it out to make sure everything works just as before. One thing we notice, though: IDE autocompletion isn't giving us our custom methods yet. Stay tuned, because in the next episode, we'll tackle editor autocompletion so you get all the nice suggestions for your custom query builder methods!