What are Eloquent global vs local scopes?
Why Interviewers Ask This
This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.
Answer
Global scopes apply automatically to every query on a model — you never need to remember to add the constraint. Create a scope class implementing Scope: class ActiveScope implements Scope { public function apply(Builder $builder, Model $model) { $builder->where("active", true); } }. Register in model's booted(): static::addGlobalScope(new ActiveScope). Remove for a specific query: User::withoutGlobalScope(ActiveScope::class)->get(). Soft delete's automatic filtering is a built-in global scope. Local scopes must be explicitly called in each query. They accept the query builder as the first argument and return it after adding constraints. Define as scopeActivePaid($query), call as User::activePaid()->get(). Local scopes accepting arguments: User::ofType("admin")->get(). Global scopes are best for security and data isolation (multi-tenancy, soft deletes). Local scopes are best for reusable, optional query constraints that make query intent explicit.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last Laravel project, I used this when...' immediately makes your answer more credible and memorable.
Previous
What is the difference between update() and save() in Eloquent?
Next
What is the Service Layer pattern in Laravel?