In this episode, we're kicking things off by talking about the basics of scopes in Laravel, especially if you're new to them. We'll use a fresh Laravel project and see how you might interact with some demo data (like users) and filter them based on common conditions — in this case, whether their email is verified.
First, we write a basic query to grab all users who've verified their email, then tidy that up by extracting the logic into a scope method right on the User model. I walk you through what a scope looks like, how to define one, and what happens under the hood when you call it in your queries. We also hit on a little quirk: scopes can't be called statically, so you need to pay attention to how they're used.
But scopes aren't perfect! We take a closer look at two of their main drawbacks. The first is that, if your app gets big (or you're working with a team), your model might start to feel cluttered if you have lots of little scope methods everywhere. The second is about developer experience — since scopes are resolved "magically" by Laravel, many IDEs won't autocomplete their names by default, making them a bit awkward to work with unless you install extra extensions.
So, scopes are a handy tool, but they do have their limits. Next up, we'll see how custom query builders can help tackle these issues and make your code more maintainable and developer-friendly as your project grows!