QA
Intermediate LevelLaravel

What is the difference between Local Scopes and Global Scopes in Eloquent?

❓ Interview Prompt: Compare Local Scopes (scopeActive()) vs Global Scopes (Scope interface, apply(), static::addGlobalScope(), withoutGlobalScope()).

1. Local Scopes ('scopeActive($query)'):

Reusable query builder methods defined on Eloquent models prefixed with 'scope'. Invoked explicitly: 'User::active()->get()'.

2. Global Scopes ('Scope' interface):

Applies query constraints automatically to ALL queries on a model (e.g. multi-tenant isolation or SoftDeletes). Bypass when needed via 'User::withoutGlobalScope(TenantScope::class)->get()'.

Code Implementation Example

php
<?php
namespace AppModelsScopes;

use IlluminateDatabaseEloquentBuilder;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentScope;

class ActiveScope implements Scope
{
    public function apply(Builder $builder, Model $model): void {
        $builder->where('active', true);
    }
}
💡Global Scope implementing Scope interface to filter active records.
🎯 Interview Key Takeaway

Use Local Scopes for explicit reusable query logic, and Global Scopes to automatically enforce constraints across all model queries.

#Query Scopes#Global Scopes#Eloquent#Intermediate
Back to all Laravel Questions→

More Laravel Interview Questions

Beginner LevelLaravel

How does the Laravel Service Container manage Dependency Injection and Bindings?

Beginner LevelLaravel

How does Routing work in Laravel including Named Routes and Route Parameters?

Beginner LevelLaravel

What is Blade Templating and how do custom directives work?