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


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.
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.
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,
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
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.
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
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,
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.
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
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
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.
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,
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
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.
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,
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,
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
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.
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.
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.
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
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
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.
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
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.
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.
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.
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
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


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!


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