What are Query Scopes in Eloquent?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Laravel development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Query Scopes allow you to encapsulate commonly-used query constraints in reusable, named methods on your Eloquent model. Local scopes are prefixed with scope and can be chained fluently: public function scopeActive($query) { return $query->where("active", true); } — called as User::active()->get(). Local scopes can accept arguments: public function scopeOfType($query, $type) { return $query->where("type", $type); } — called as User::ofType("admin")->get(). Global scopes apply automatically to every query on the model (e.g., soft delete filtering). Create a global scope class and register in booted(): static::addGlobalScope(new ActiveScope). Global scopes can be removed for a query with ::withoutGlobalScope().
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.