This episode is for members only

Sign up to access "Build a Referral System with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
17. Excluding cancelled subscriptions

Transcript

00:00
OK, so just before we go on to handling future payments, we really want to get rid of active referrals under our stats dashboard if they've
00:07
canceled their subscription. If they've canceled, they're not going to be billed again, so there's probably no point even showing them here. And that will also lead us into the display of this
00:16
when we don't have any active referrals, which will just tidy up in our view. OK, so let's take a look at manually cancelling a subscription in the database since we don't have
00:26
the functionality for this. So when a user cancels their subscription, endsAt is going to be set to a specific date in the future when this actually ends.
00:36
So I'm going to set it to now, but then I'm just going to slightly increase this maybe to a couple of days. OK, so now that we've done that and we come over, this subscription has been canceled,
00:45
but it's still showing under active referrals, which is not what we want because we're not going to receive any more money from this now it's been canceled.
00:52
It will re-show up if they go ahead and resume their subscription, but at least at this point in time, we shouldn't show it. OK, so let's figure out how we can do this.
01:03
We'll go over to the subscription model over in Cachier, and let's just have a look at the method list. Now, I'm going to guess that there's
01:10
a scope in here which will scope this by a particular status. So for example, if we just type scope in, this will show all of the predefined scopes that have been included.
01:20
So you can see here that we have actually got a scope not canceled. So this will only give us back subscriptions where the ends at date is null, which is exactly what we want.
01:31
So we can either use this scope, or we can just use the where null on our own. But since this is part of the model, we may as well use it. So we're going to come over to our referral dashboard
01:40
controller, and we're going to output all of the subscriptions in here. Rather than doing all of these scopes within the view, it doesn't really make sense, and it's going to get messy.
01:50
So I'm going to go and output subscriptions, and we're just going to do the same thing. We're going to say auth user referral code, and we're going to say subscriptions,
01:58
and we're going to say get. And we could order these as well if we wanted to. So let's just do that properly, and let's just swap this over first of all.
02:07
So let's go down to our referral subscriptions, and we'll just swap this over to use that subscriptions value. Give it a refresh. We get exactly the same thing.
02:17
OK, so now that we've done this, I'll let you go ahead and order this if you want to by a specific column. But we can now use that scope that we found over here,
02:26
which is, if we just remind ourselves, not canceled. So all we need to do to use a scope is just use anything after the scope keyword. So we're just going to say not canceled and get.
02:37
So if we come over and give that a refresh, I think we've spelled canceled wrong probably. Let's just see. Yeah, it's just one L within the thing.
02:45
So there we go, not canceled, and there we go. So we now see none because that has been canceled. So let's go ahead and just finish up now that we have done that by just making
02:56
a sort of empty state in here for if we don't have anything. So we can actually use a for else in here, and then we can say empty, and then we can end the for else. This is just a convenient way of iterating,
03:14
but also having a kind of empty state as well. So if this is the case, let's go ahead and create a table row with a TD three times, and then we can just add in a hyphen in there, and then maybe set the text color
03:29
to gray for each of these. So let's say text gray 500. OK, let's go over and have a look, and there we go. So now we can actually see that we've not got anything.
03:40
OK, so I'm going to go ahead and uncancel that, and of course, that's now going to show back as the active referrals. Now, the conversions doesn't change
03:47
because we've technically converted them in the past, but now we've tied up active referrals to not show people who have canceled their subscription. This makes a lot more sense.

Episode summary

In this episode, we're cleaning up our stats dashboard so that it only shows active referrals. That means if someone has canceled their subscription, they won't appear in our "active referrals" list anymore—after all, we're not going to earn any more from them!

We start by manually canceling a subscription in the database (since our app doesn't have a UI for that yet) and then checking the dashboard. As expected, even a canceled subscription keeps showing up, which isn't what we want. Next, we dive into our code and look for ways to filter out canceled subscriptions. We discover that our Subscription model has a handy notCanceled scope already set up, which filters out subscriptions that have an end date (meaning they're canceled).

With just a small tweak in our controller, we use this scope so our dashboard only fetches active subscriptions. We also handle the empty state by using a nice Blade @forelse loop, so if there aren't any active referrals, the dashboard won't look broken — it'll now show a helpful empty message.

At the end, we demonstrate how the list updates when a subscription is uncanceled, making sure everything works the way we expect. This tidy-up makes the dashboard make a lot more sense for users and keeps things accurate!

Episode discussion

No comments, yet. Be the first!