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
06. Showing the referrals dashboard

Transcript

00:00
OK, let's go ahead and build out a referrals dashboard and then prevent the user accessing it if they haven't added a PayPal email address.
00:07
OK, so let's create out a really quick controller for this. So let's make a controller called Referrals Dashboard Controller. And let's go ahead and register this under our route.
00:21
The user, of course, needs to be signed in here. So we're going to put it under this Auth Middleware group. So let's go and create out a get route for slash referrals, hook up to our Referral Dashboard Controller,
00:35
and we'll give this a name. And let's just call this Referrals and Index. OK, so let's head over to our Dashboard Controller. And we can pretty much just make this really simple
00:47
by returning a view for now. Of course, we're going to be passing data down a little bit later. But let's create out this view.
00:53
So let's say phpArtisan make view. And let's call this Referrals and Index. We might get into a little bit of trouble later when we look at the actual referral pages
01:03
when we copy the link. But we should be good. So that's going to be Referrals and Index. And if we head over to that Referrals Index page,
01:12
we can just basically paste in what we already have on our dashboard.blade.php view just to save a little bit of time. So this is going to be our Referrals section.
01:22
And we'll just write Referrals in there. Great. OK, so that should be good. I should now be able to head over to Referrals
01:28
and see my referrals. Brilliant. OK, so we'll add this to the menu. But then we will, first of all, hide it from the menu
01:34
if we don't have a PayPal email address. And we'll also add some middleware to stop the user accessing this page if they don't have a PayPal email address filled.
01:42
OK, so the first step is heading over to the Navigations. So this uses our base app layout. So it's just here. And we have this Navigation here as well,
01:52
which contains stuff like the Profile dropdown link that we have over here. So we're pretty much just going to go ahead and grab this and just create our Referrals link
02:01
and link that over to Referrals and Index. So now we can, of course, go ahead and click on this. But we want to get rid of this if the user doesn't have a PayPal email address.
02:11
So I'm going to go ahead and manually get rid of this from the database since we can't do it from our app. And we basically just want to hide this. So a very easy way to do this now under our Has Referrals,
02:22
we have Referrals enabled. We can just very easily go ahead and use this. So let's wrap this in an if statement and say if AuthUser Referrals is enabled,
02:34
then we show that menu item. And there is a mobile version of this as well if you wanted to go ahead and add that. But of course, it's probably going
02:42
to be different for everyone. OK, so this isn't working at the moment. And I have a feeling it's just because we have empty in here for the null.
02:48
So let's go ahead and specifically set this value to null and save that out. And there we go. We do not see our Referrals menu option.
02:56
Of course, as soon as we fill our PayPal email address, we know that we have a referral code. Once the PayPal address is in there, it's going to work. But we can still access this referral route.
03:07
So let's build out some middleware to prevent the users from accessing this. So let's say make middleware. And we're going to go ahead and say redirect
03:16
if no referral code. Of course, call that whatever you want. OK, so let's go over to our redirect if no referral code middleware and think about what
03:25
we need to do in here. Now, we're always going to assume that the user's signed in when this middleware is applied. So I'm not going to do an auth check in here.
03:33
But we basically just want to say from the request, if the user doesn't have a referral code, so let's just remind ourselves of referrals enabled. And if it's not enabled, we just want
03:45
to redirect the user somewhere else. So we could go ahead and redirect them over to any route. But I'm going to choose to redirect them over to the profile edit page so they can see that.
03:55
And of course, you could flash a message as well if you wanted to let them know. So let's go ahead and add this middleware just specifically to that route.
04:02
We could do that at the route level or in the controller. So I'm just going to attach it on with middleware here. And we're going to say redirect if no referral code. And we're pretty much done.
04:13
So now when we head over, that's automatically redirected because everything got refreshed. But I cannot access referrals now. Can't see it in the menu until I've
04:22
entered my PayPal email address. So let's finish up by just adding in my PayPal email address. Of course, I can now access our referrals.
24 episodes2 hrs 39 mins

Overview

Let's build a feature complete Laravel referral system, completely from scratch.

We’ll cover generating referral codes, setting a cookie to track the referral process, hooking up referrals to subscriptions and displaying detailed referral stats in a dashboard, so your users can see how they're doing.

On the admin side, we'll set up a job to automatically generate a CSV with all the amounts you need to pay out each month, automatically mark referrals as paid, and display historical referral payments for users.

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

Comments

No comments, yet. Be the first to leave a comment.