در حال بارگذاری ...

Creating Members Section0:00

Okay, let's see how we can check the subscription status of the currently logged in User. So I want to be able to have a members only section that's only accessible to people who have paid. So let's go ahead and do that. So I'm going to create a new section here for members only. So let's create that new menu item. Let's call it members. Okay. And let's go into our routes file. Let's copy this, paste it in here, and let's just name it members.

And let's go into our routes/web.php file. Let's copy this, paste it in here, and let's just name it members. Okay. And let's go ahead and create that view. So I'm just going to duplicate this one, call it members. And let's just put members in here. Okay. Members only. And this should render, but it's accessible to everyone right now. And we want to lock it down to people who have only paid.

Restricting Access Middleware0:55

And this should render, but it's accessible to everyone right now. And we want to lock it down to people who have only paid. So to do that, if you look at the docs, we can make use of this subscribed method and give it the product name. And if you scroll down here, you'll see an example for middleware. So let's grab this. Let's make a new middleware, php artisan make:middleware. Let's call it PayingCustomer. Okay. And let's paste that in PayingCustomer, paste that in here.

Okay. And let's paste that in payingCustomer, paste that in here. And as you see, it's checking if the User is subscribed to, in this case, we named it cashier. And if they're not redirected to a billing page, so we called it subscribe. Okay. So now let's go to our Kernel and register that middleware. Let's put it right here. Let me just copy this one actually. And let's call it payingCustomer.

Let me just copy this one actually. And let's call it payingCustomer. And this is payingCustomer. Okay. And now we can go back to our routes file and wrap this route in payingCustomer. Okay. So now this should still render because the User I'm logged in as is a payingCustomer from the last video. But if I log out and I believe I have another User here who is not subscribed, then it should redirect them back to the subscribe page.

Redirecting Already Paid Users2:28

But if I log out and I believe I have another User here who is not subscribed, then it should redirect them back to the subscribe page. And it does. Cool. Now, if you want to check if they're subscribed to a specific plan, if you remember, we have these two plans, then you can do that with the subscribedToPlan method, but we'll just keep it to this method for now. So we probably also want to handle the case where they are already subscribed and we don't want to show this page. So it's similar to the redirectIfAuthenticated middleware.

want to show this page. So it's similar to the redirectIfAuthenticated middleware. So this one right here. So let's go ahead and make that. So I'm going to duplicate this payingCustomer and let's call it redirectIfPaid.php. Okay. And all we have to do is change this and redirect them to the member section. Okay. Now we can go in here and register it. So let's just duplicate this.

Now we can go in here and register it. So let's just duplicate this. It's redirect if paid. If paid. And let's call it non-payingCustomer. Okay. Now in our routes file, we can wrap whatever routes we want to wrap it in. So in our case, we want these two. So non-payingCustomer. Okay.

So non-paying Customer. Okay. So now, and I think I forgot to change the class name. So let's change the class name here. Yeah. Right here. So redirect if paid, refresh. And since I'm not paying, then I'm able to access this page. So again, let me log in as the other User. And if I press subscribed, since I am subscribed already, it should redirect to members and

Conditional Navigation Items4:31

So again, let me log in as the other User. And if I press subscribed, since I am subscribed already, it should redirect to members and it does. So one more quick thing here. I just want to show and hide the menu items based on if they're subscribed or not. So let's do that. Let's go into our, where is it? NavigationDropdown. And let's wrap this one in a conditional. And we can just check if the User is subscribed.

And let's wrap this one in a conditional. And we can just check if the $user is subscribed. So off user subscribed to Cashier. And this one is actually not. Right. That. Okay. And the other one is the opposite case. So let me just grab this and put it for this one members. So if we are subscribed and if here and that should do it, you can also put this in Blade.

So let me just grab this and put it for this one members. So if we are subscribed and if here and that should do it, you can also put this in Blade directives like Laravel's @if and @guest directives, but this is fine for now. This should hide. Sorry. Do I have a typo? I do. Okay. And you see the subscribe menu item is hidden because I'm already subscribed. So yeah, definitely make use of these methods to check if your users are subscribed or not.

Subscription Checks and Authorization5:54

And you see the subscribe menu item is hidden because I'm already subscribed. So yeah, definitely make use of these methods to check if your users are subscribed or not. You can also combine it with gates or policies to limit your users to certain features based on their plan or their subscription status.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟