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); }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners