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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners