DEV Community


Discussion on: The magic of query scopes in Laravel

nate6762x profile image
Nate Granger

"Any useful tips for beginners missing in this article?" Really?
I'm one of your “beginners,” a laravel noob (3 months full time), 2 years studying webdev & OOP on my own. This article is exactly the kind of ubiquitous “training” that frustrates me. From what I can tell, this column is only for experienced programmers, and they probably don't need it anyway. Maybe at best it would be something confirming for them to read.
Why, you may ask? Nowhere in this article do you define “query scope.” You don’t say what it is; you don’t point to it; you suggest how it helps you but are not specific how that happens. You present 12 lines of code, and say “look at” it. It’s like teaching landscape painting by taking the student outside, gesturing at the view, and saying “See?” No, I don’t see. You don’t explain what was done or why and how it was an improvement. So you’re right, it truly is magic; I see an outcome and have no idea what trick you used to get there. Some of these things I might guess at, but was that your intention? If it’s for beginners, wouldn’t you expect it to be explicit? (eg “They removed X from the query and put it in its own separate function, … and that is why it’s known as query scope”.) After dissecting this article (for the purpose of this comment), I haven’t the slightest idea of what query scope is, when, where, why or how to use it. I know what a scope is, and what a query is, but taken together they are apparently a term of art whose meaning cannot be deduced from its parts.
I am pushing 70, have decades of work experience, much of it in training. I’d love to have your knowledge to be able to impart it to others, but I could certainly help you do it if that’s your actual intention. Also, be aware it’s not just you. Almost EVERY such “explanation” out there has the same flaw, particularly the Laravel docs.
Please take this as constructive (albeit harsh) criticism, meant to improve your effort to share your gift. If you would like to correspond further, I would be happy to engage.

bertheyman profile image
Bert Heyman Author • Edited

Hi, thanks for sharing your feedback! I'll have a look where some more context might be useful.

Actually, query scopes might not always be the right choice. They're merely a syntax that allows you to split queries into small, clean and reusable blocks. This might improve the readability and allow for easier tweaks at a later time.

If you are interested in learning the "why" and not just the "what" and miss this in the docs, Laracasts might be a great resource on your journey of learning Laravel. Good luck!