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
10. Setting the referral cookie

Transcript

00:00
OK, so now when the user submits this form or just clicks
00:03
this button to submit the form, we want to go through to another route. We want to post through to another route to set the cookie and have that cookie available.
00:11
So what we'll do is go through and actually post this through. We will set the cookie. We'll also increment the clicks count as well, which is really important.
00:20
And then on the dashboard, we'll show a message if that cookie has been set. So a few things to do. Not too much trouble.
00:26
So let's go and start with a controller to go ahead and actually store this. So we'll call this referral store controller. And let's go over to our routes.
00:36
Again, we don't need to be authenticated for this. So we will just go and post over to referrals. We still need the code in there. That's really important.
00:46
And referral store controller. And of course, we'll call this referral and store. So let's grab this. And we will just put this directly
00:55
into the action of here within a route, not forgetting to pass through the referral code itself so that route can be built up. And we'll also set the method in here
01:05
to post by default. That's going to be get. OK, so we should now be able to go through to here. Of course, we just need to fill in a invoke method in here. So let's go to our referral store controller
01:16
and just pull in invoke. Make sure we get through the actual referral code itself. And again, just for now, we'll go ahead and die dump on that referral code.
01:26
Great. OK, so we can click this now and go through. Brilliant. So the first thing is pretty easy.
01:32
We just want to go ahead and increment the clicks. So very easy to do. Referral code, increment, and we'll increment those clicks. And then we want to set a cookie.
01:43
So this is slightly more difficult, but not too bad. So we're going to say cookie, and we're going to queue a cookie. There's a couple of ways
01:50
that you can set a cookie in Laravel. You can do this as you redirect. So eventually, we're going to redirect the user over to the register page so they can create an account
02:00
and then go ahead and buy a subscription. So you can say with cookie at the end of here and do everything we're about to do in queue directly in here.
02:09
It's really just personal preference. But I'm just going to do this directly up here so it's a little bit clear what we're doing. OK, so in here, what we want to do is queue out a new cookie.
02:19
So we're going to new up an instance of cookie. We're going to call this referral code, and we're going to give the value of our cookie. So this is just going to be the referral code's code
02:29
that we get in the database. Now, the next thing is going to be the expiry. So this is in minutes. So we're going to say now, we're going to say add month.
02:38
This will give us back a carbon instance. We'll add a month to it, and then we'll use diff in minutes. And there we go.
02:46
That's it. So we create a cookie. We queue it so when we redirect, that gets set. Or like I said, you can use with cookie
02:54
to do all of this inside of there. OK, so let's go over, grab our referral code, and I'm going to open up a new browser here and just paste this in.
03:03
I'm then going to click sign up for 20% off, and of course, we're redirected over to register. So let's go ahead and register a new account since we've just clicked on that.
03:12
And of course, with the standard flow, we're going to end back up on our dashboard. This is where we now want to show whether this cookie has been set or not.
03:21
So if we go over to the dashboard.blade.php file, this is pretty straightforward to do. Now, at the moment, if we just have a look at our web routes,
03:31
our dashboard isn't within a controller, which makes things a little bit messy. So what I'm going to do is actually go ahead and build out a controller for this.
03:39
So let's go and make out a dashboard controller just so this is a little bit neater. So dashboard controller, and we'll just swap this over
03:48
to use that dashboard controller. And we'll pretty much just do exactly what we had within there. So we'll build out an invoke magic method
03:57
and just return that dashboard. So this should look exactly the same. If we swap over here, there we go, great. Okay, so in the controller now,
04:06
what we want to do is pass down this referral code. So let's go ahead and say referral code, but we don't just want to pass down the cookie value. We want the instance of the referral code
04:15
so we can check it properly. We might have other things that we want to show here. So we're just going to say referral code, and we're going to say where code is,
04:26
and we need to grab this from the cookie. So what we do is we use our request helper or directly from the request that's injected in, and we just say cookie,
04:37
and then the name of the cookie that we set, which is referral code. And then we can just say first. Now that might not be available.
04:44
The user might not have this set, in which case this is going to be null, which is fine because we're going to wrap this in an if statement.
04:50
So now over on our dashboard page here, we could add a little if statement in here to say if referral code, and we'll spell that properly.
05:02
And in here, we're just going to add in a paragraph, and we'll just say you'll receive a discount due to a referral. So basically, inside of this entire dashboard section,
05:16
that's where our plans are going to go. Of course, this would eventually go on a dedicated plans page where you show all the plans that a user can sign up for,
05:24
but for now, this is pretty fine. Okay, so now over here on our browser where we've been referred, you can see that it says you'll receive a discount
05:33
due to a referral. However, on our dashboard, of course, we don't see that because that's not been set. So we now know whether a user has been referred
05:43
which means that when they go ahead and go through to the checkout link, we can create a specific coupon for them to go ahead and apply that coupon to their first purchase.
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!

Episode discussion

No comments, yet. Be the first!