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!