I have a Subscription model which contain start_date
and end_date
attributes in my Laravel app. I have created two query scopes, scopeActive and scopeFuture to find active and future subscriptions (respectively).
I would like to know how I can build a query using both scopes in OR context, so that I can find any active or future subscriptions.
Subscription Model
/**
* Scope a query to only include active subscriptions.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActive($query)
{
return $query->where('start_date', '<', Carbon::now())
->where('end_date', '>', Carbon::now());
}
/**
* Scope a query to only include future subscriptions.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeFuture($query)
{
return $query->where('start_date', '>', Carbon::now());
}
public function business()
{
return $this->belongsTo('App\Business');
}
Business Model
.....
public function subscriptions()
{
return $this->hasMany('App\Subscription');
}
User Model
public function business()
{
return $this->hasOne('App\Business');
}
Controller
Subscription::active()->get();
$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).
$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).
Subscription::active()->orWhere( function($q){
return $q->future() ;
} )->get() ;
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With