Easy Global Scope

Submitted by jarektkaczyk - 3 years ago

The easy way to define Eloquent Global Scopes with https://github.com/jarektkaczyk/laravel-global-scope

<?php

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Sofa\GlobalScope\GlobalScope;

class YourScope extends GlobalScope
{
    public function apply(Builder $builder, Model $model)
    {
        // any constraints on the builder here, for example short query like this :)
        $builder->where(function ($q) {
            $q->whereHas('someRelation', function ($q) {
                $q->where('some_col', 'some_val')
                    ->whereNull('another_col');
            })->orWhere('something_else', true);
        });
    }
    
    // NO NEED TO DEFINE REMOVE METHOD - IT WILL WORK AUTOMATICALLY
}
comments powered by Disqus