Ever found your self repeating a query for a specific role, as an example. Lets put a scenario where we actually need a global scope, we have an orders table, there is free orders and payed orders; Clients can see only their orders. Commercials can see orders of their clients.
First thing we create a scope lets call it App\Scopes\OrderScope.php isClient()) { $builder->where('user_id', Auth::id()); } elseif (Auth::user()->isCommercial()) { $builder->whereHas('user', function (Builder $query) { $query->where('commercial_id', Auth::user()->id); }); }} }
use App\Scopes\ComplaintScope; //add that function to the model protected static function booted() { static::addGlobalScope(new ComplaintScope); }