🔴 Laravel
Beginner
What are Query Scopes in Eloquent?
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().