The same three conditions appear in nine controllers, and when the definition of “active” changes there are nine places to edit and one that gets missed.
class Order extends Model
{
public function scopeOutstanding($query)
{
return $query->where('status', 'paid')->whereNull('shipped_at');
}
public function scopeForCustomer($query, Customer $c)
{
return $query->where('customer_id', $c->id);
}
}
Order::outstanding()->forCustomer($customer)->get();
The naming rule is that scopes describe a domain concept, not a column — outstanding() rather than whereStatusPaid(), or the scope is just a rename of the thing it wraps. Global scopes exist too and are worth avoiding until you have been bitten once: a query that silently excludes rows is very hard to debug from the call site, which is exactly the point of them.