Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Global Scope Laravel Package

sofa/laravel-global-scope

Deprecated: Laravel 5.2+ changed global scopes, so this package is no longer valid. For Laravel 5.0–5.1, it provides an abstract Eloquent GlobalScope to define scopes via apply() and makes removing scopes from queries easier.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Legacy Laravel Fit: Designed for Laravel 5/5.1, but obsolete due to Laravel 5.2+ native global scope support. No architectural alignment with modern Laravel (8.x/9.x/10.x) or Eloquent conventions.
  • Scope Abstraction: Provides a thin abstraction over Eloquent’s GlobalScope, but adds no meaningful value beyond basic apply() method enforcement. Modern Laravel’s built-in GlobalScope is functionally equivalent.
  • Design Pattern: Follows a simple decorator pattern, but lacks features like dynamic scope removal, conditional application, or query caching optimizations.

Integration Feasibility

  • Zero Integration Value: Since Laravel 5.2+, global scopes are natively supported via addGlobalScope() and withoutGlobalScopes(). This package reduces to a single abstract class with no additional utility.
  • Breaking Changes: Requires PHP 5.4+ and Laravel 5.x, making it incompatible with modern PHP (8.x) or Laravel (8.x+). Migration would require rewriting scopes to use native Laravel features.
  • Dependency Risk: No active maintenance (archived), no dependents, and a MIT license (no legal barriers, but no guarantees of stability).

Technical Risk

  • High Risk of Obsolescence: Native Laravel global scopes are superior in performance, maintainability, and compatibility. Using this package would lock the project into legacy Laravel versions.
  • No Testing/Documentation: Minimal README, no tests, and no examples for modern Laravel. Risk of hidden bugs or edge cases (e.g., scope removal logic).
  • Performance Overhead: Abstract class adds negligible overhead, but native scopes are optimized for Laravel’s query builder.

Key Questions

  1. Why not use native Laravel global scopes? (Answer: This package offers no advantage.)
  2. Is legacy Laravel 5.x support a hard requirement? (If yes, assess migration costs to modern Laravel.)
  3. Are there undocumented features (e.g., query macros, dynamic scope toggling) that justify the risk?
  4. What’s the long-term maintenance plan? (Archived repo = no updates.)
  5. Does the team lack familiarity with native global scopes? (If so, training may be a better solution.)

Integration Approach

Stack Fit

  • Incompatible with Modern Stacks: PHP 5.4+ and Laravel 5/5.1 are deprecated. No support for:
    • Laravel 8.x+ (query builder improvements, Eloquent enhancements).
    • PHP 8.x (named arguments, union types, attributes).
    • First-class dependency injection (Laravel’s container improvements).
  • Alternative Stacks: If using Lumen or Laravel 5.2+, native global scopes are the only viable option.

Migration Path

  1. Assess Current Usage:
    • Audit all global scopes in the codebase. Identify if they rely on package-specific features (e.g., remove() helpers).
    • Check for custom query macros or builder extensions.
  2. Replace with Native Scopes:
    • Convert Sofa\GlobalScope classes to Laravel’s Illuminate\Database\Eloquent\Scope:
      class PublishedScope implements Scope
      {
          public function apply(Builder $builder, Model $model)
          {
              $builder->where('published', true);
          }
      }
      
    • Register via addGlobalScope() in the model’s boot() method:
      protected static function boot()
      {
          static::addGlobalScope(new PublishedScope);
      }
      
  3. Handle Scope Removal:
    • Use withoutGlobalScopes() for dynamic removal:
      Model::withoutGlobalScopes(PublishedScope::class)->get();
      
  4. Deprecate Package:
    • Remove sofa/laravel-global-scope from composer.json and update dependencies.

Compatibility

  • No Backward Compatibility: Laravel 5.2+ deprecated this package’s approach. No migration path exists for features beyond basic apply().
  • Testing Required: Verify all queries behave identically after migration (e.g., eager loading, relationships, soft deletes).

Sequencing

  1. Phase 1: Replace one scope at a time, testing queries in isolation.
  2. Phase 2: Update CI/CD to use Laravel 8.x+ and PHP 8.x.
  3. Phase 3: Deprecate and remove the package entirely.
  4. Phase 4: Refactor remaining legacy code (e.g., custom query macros).

Operational Impact

Maintenance

  • Zero Maintenance: Archived package with no updates. All maintenance shifts to native Laravel scopes.
  • Reduced Boilerplate: Native scopes require less abstraction, making them easier to debug and extend.
  • Community Support: Native Laravel scopes are widely documented and supported (e.g., Laravel News, Stack Overflow).

Support

  • No Vendor Support: Package author is inactive. Issues must be resolved via native Laravel docs or community.
  • Debugging Complexity: Native scopes integrate seamlessly with Laravel’s debugging tools (e.g., QueryException, dd($query->toSql())).
  • Training: Team may need training on Laravel’s Scope interface, but this is a one-time cost.

Scaling

  • Performance: Native global scopes are optimized for Laravel’s query builder. No additional overhead.
  • Concurrency: No impact on scaling; both approaches use the same underlying query builder.
  • Database Load: Proper indexing and query caching (e.g., query() method caching) work identically.

Failure Modes

  • Query Regression Risk: Incorrect scope implementation could break queries (e.g., missing where clauses, N+1 issues). Native scopes force explicit query building, reducing ambiguity.
  • Scope Leakage: Accidental global scope application (e.g., forgetting withoutGlobalScopes()) could cause performance issues. Native Laravel provides clearer error messages.
  • Migration Failures: If scopes rely on package-specific features (e.g., remove()), migration may require significant refactoring.

Ramp-Up

  • Low Learning Curve: Native Laravel scopes are straightforward. Example:
    // Before (package)
    class PublishedScope extends Sofa\GlobalScope {
        public function apply(Builder $builder) { ... }
    }
    
    // After (native)
    class PublishedScope implements Scope {
        public function apply(Builder $builder, Model $model) { ... }
    }
    
  • Documentation: Laravel’s official docs are comprehensive.
  • Tooling: IDE autocompletion and Laravel’s make:scope artisan command (if available) accelerate development.
  • Testing: Write unit tests for each scope using Laravel’s QueryBuilder assertions:
    public function test_global_scope()
    {
        $query = Model::query();
        $query->getQuery()->expects($this->once())->method('where')->with('published', true);
        Model::all();
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
amashukov/lnd-client-php
althinect/enum-permission
andydefer/laravel-actions
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor