Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.0] Add subscription state scopes #609

Merged
merged 1 commit into from
Feb 23, 2019
Merged

[9.0] Add subscription state scopes #609

merged 1 commit into from
Feb 23, 2019

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Feb 19, 2019

I've been using these in my applications and I had someone else ask about them, so I thought I would PR them and see if they might be handy having in the base package.

You can now apply query scopes based on the state of the subscription.

On trial

$users = User::whereHas('subscriptions', function ($query) {
    $query->whereName('gold')->onTrial();
})->get();

Active

$users = User::whereHas('subscriptions', function ($query) {
    $query->whereName('gold')->active();
})->get();

Cancelled

$users = User::whereHas('subscriptions', function ($query) {
    $query->whereName('gold')->cancelled();
})->get();

On grace period

$users = User::whereHas('subscriptions', function ($query) {
    $query->whereName('gold')->onGracePeriod();
})->get();

Ended

$users = User::whereHas('subscriptions', function ($query) {
    $query->whereName('gold')->ended();
})->get();

Recurring

$users = User::whereHas('subscriptions', function ($query) {
    $query->whereName('gold')->recurring();
})->get();

There are a couple of other added scopes, but they are just there to let these 👆ones work nicely under the hood.

These can come in handy when you want to email all your subscribers that perhaps have cancelled - or send a thank you for all your recurring subscribers once a year, etc.

// These users signed up and didn't even finish the trial before they cancelled.
// We might wanna reach out and see if they had any issues with the service.

$users = User::whereHas('subscriptions', function ($query) {
    $query->whereName('gold')->onTrial()->cancelled();
})->get();

As pointed out on Twitter this would also be handy for reporting.

src/Subscription.php Outdated Show resolved Hide resolved
@driesvints driesvints dismissed their stale review February 21, 2019 22:23

Changes were done.

@taylorotwell taylorotwell merged commit 7faa376 into laravel:9.0 Feb 23, 2019
@taylorotwell
Copy link
Member

Pretty cool, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants