Using multiple Laravel scopes in OR context
Take advantage of params. You could try something like...
public function scopeCanceled($query, $scope = 'where')
{
return $query->$scope('canceled_at');
}
then you can do for example $query->canceled('orWhere')
you can then de-uglify with:
public function scopeOrCanceled($query)
{
return $this->scopeCanceled($query, 'orWhere');
}
example $query->orCanceled()
in general with scopes, i find it handy to always have some sort of optional value, to make things flexible
public function scopeApproved($query, bool $value = true)
{
return $query->where('is_approved', $value);
}
adding a third param for this use case can then make sense
public function scopeApproved($query, string $scope = 'where', bool $value = true) {
return $query->$scope('is_approved', $value);
}
Subscription::active()->orWhere( function($q){
return $q->future() ;
} )->get() ;
For static-context try:
$subscriptions = Subscription::query()
->active()
->orWhere(function ($query) {
$query->future();
})
->get();
First call
query()
to prevent errors (because we may have a non-static method with "active
" as name).
Relation query (as requested in the comment)
$subscriptions = auth()->user()
->business()
->subscriptions()
->active()
->orWhere(function ($query) {
$query->future();
})
->get();
Note that above we don't need to call
query()
(because it's not from static-context).