This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
11. Checking if a customer is subscribed

Transcript

00:00
Let's look at some of the methods that we can use to check if a user is subscribed, whether they have cancelled their subscription, and then once again, you can either dive into the documentation or the source code to find out any of the other methods that you can use. Okay, let's just do this on the dashboard for now, just to keep things really simple,
00:17
and we'll perhaps just do some dumping of this data out. Okay, let's actually open up the home controller because we have a controller for this, and let's either bring in the request up here to access the user, or of course, once again, use auth and user to access any of the methods on that billable trait. Okay, so I'm going to head over to the home page, and let's go ahead
00:41
and take a look. Okay, so we're going to go ahead and die dump here on the user from the request, and we have a really simple subscribed method in here, which will return a true or a false value, so a boolean, whether or not we're subscribed. Now, remember a little bit earlier over in the checkout controller, we set up the default tag for this subscription. That is the
01:06
default value that's used. If you had multiple subscription types, and you wanted to check if a user was subscribed to a particular type of subscription, you could go ahead and pass that in there. For now, we can either pass default in, or we could just get rid of this since it's assumed that we're working with that default tag. Okay, let's go ahead and die dump on this,
01:26
and see what we've got. Now, obviously, we've got false because I cancelled that subscription a little bit earlier over in here, but you get the idea. This will return a true or a false value, whether we're subscribed or not. We also have the ability to check if a user has cancelled their subscription, but we need to do this directly on the subscription object. So once again, let's
01:47
just dive into that billable trait, and let's go over to, I think it's under manages customer. Let's go ahead and open up the method list here, and look for subscribed. And yep, sure enough, it's not in there. So let's have a look, probably under here. Let's look for subscribed. And yeah, there we go. So that's the method that we have currently been using. But what we want to do
02:09
is find out whether a user has cancelled their subscription. So to do this, what we actually need to do is access the subscription itself. So use the subscription method. And again, we can pass through the type here if we want to. And then onto this subscription method, which actually returns to us a cashier subscription model. We can use any of the methods on there. So
02:31
if we go ahead and look for our subscription model, and we'll choose all places here, we should have a subscription model in here under the cashier package. So again, this gives us things like whether this is cancelled, or we can use methods like cancel to actually cancel the subscription at the end of the billing period, or cancel now. So some of these methods are available
02:55
as helpers directly on the user, like whether they're subscribed or not. But some you'll have to access the subscription first, and then use this method. So like cancelled. Let's take a look and see. So we've got true. Of course, we did cancel that subscription. This will be true whether the user has chosen to cancel it, or it is cancelled. So you can use whatever makes sense
03:18
there. Okay, so now that we've figured this out, let's over the next few episodes do a few more useful things like create a protected area with middleware, show an item in the navigation, and obviously allow the user to eventually with the methods that we've seen, manage their subscription.
26 episodes2 hrs 38 mins

Overview

Learn to start accepting subscriptions with Laravel and build a solid foundation for your next application. In fact, it’s exactly how we do things here on Codecourse!

Even if you’re completely new to Laravel, by the end of this course you’ll have a platform where customers can purchase subscriptions and gain access to restricted areas of your application.

We’ll also cover managing subscriptions, cancellations, invoices, giving customers trial periods — and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!