Playing
03. Generating unique referral codes

Transcript

00:00
okay so we know that when we create the referral code schema we set the code to be unique but when
00:06
we randomly generate this within laravel and insert it into the database there is going to be a chance that the two collide very very slim chance but we want to make sure that this doesn't error out when we're doing this so there's a really easy technique that we can use to do this okay so let's go over and look at creating an observer for our referral code now the reason
00:26
i'm attaching an observer to the referral code is if we generate our referral code in the database manually or anything i want this functionality to be tied specifically down to creating a referral code so what we're going to do is create out an observer and let's go and create that for our referral code so we'll call that referral code observer and let's go ahead and open up that
00:48
referral code observer and we'll create out a created method so let's say created or this is going to be creating because we want to do this while it's being created and into here we will get the referral code itself and then what we can do in here is fill in the code manually with a uniquely generated code let's just see how that's going to look first of all so let's say referral
01:13
code equals and let's say string random and let's do like a six character random code so now what's going to happen if we just boot up a tinker session when we create this out so let's say app models referral code and create now we can just use create on its own here because the code's going to get filled with this when we do this we don't need to fill in the views or the clicks or anything
01:37
like that and in fact before we do this let's hook up the actual observer itself so we'll do this under the event service provider so we're basically saying here when we boot this we want the referral code to have this observer so let's pull in the referral code observer in here so whenever something happens on here the methods in here the eloquent methods are going to kick in
02:02
okay so now that we've done this let's exit out of tinker because that's going to need that new code and let's say app models referral code and we'll just create that out okay so user id doesn't have a default value of course so let's just fill that in really quickly with my user id and we should be good great so you can see that has generated out a unique referral code so that's
02:27
the first step we're generating unique referral codes whenever a referral code gets created i'm going to bump that up a tiny bit to eight and now what we'll do is look at the collision stuff here so what we can do is extract this out to a new class you could do everything in here but things are going to get a little bit messy so we're going to go and create out a support folder
02:50
of course you can put this wherever you want but this is going to contain a referral code generator it's going to be a really really simple class so let's just start to scaffold this out and this is going to be under support referral code generator and this is just going to have a generate method in so let's just create that out
03:16
and that's going to generate out and return to us a referral code so the way that we're going to use this in here instead of doing this is we're going to new this up so create a new referral code generator and then we will invoke that generate method and then of course from here we can return that random string but in the meantime up here we can check if this exists
03:39
first of all and we can loop through so let's just make sure that all of this is working so let's x out and just try this one more time and there we go we get the code abc because of course i've just hard-coded that return in there okay so how do we generate our code and check for collisions at the same time well the first thing that we do is generate out the code up here so we assign this
04:00
to a variable so let's take that random string and then let's return it at the bottom now in here in the meantime what we can do is we can check that a referral code already exists so we can say while and the condition of this is while this exists in the database so we might have a code generated that already exists then the next loop we might have another code generated that already exists
04:26
although that's going to be incredibly uh rare that probably wouldn't happen but then when it gets to the point where we don't we can then go ahead and just fall back and generate the code so in here let's say referral code where code equals the code that we've already generated and we can use an exists method in here which is really handy so while this exists keep generating
04:48
a new code so in here we're going to say code equals and then we're just going to do the same thing and we could extract this out to a method as well that'd probably be a good idea so let's go ahead and say generate code and we'll just return this from here just so we don't have to repeat ourselves and then in here we'll say this generate code and then we'll do exactly the same thing here
05:14
as well great okay so we generate our code but then while this exists if it exists keep generating a new code until it doesn't exist and then return the code so hopefully that makes sense okay so now that we've done this let's go ahead and do the same thing in tinker again and we'll just go and exit out of here try this again and there we go so we're generating a unique code so that is working
05:39
it's really difficult to test the while loop here to check that it does exist but that's going to go ahead and continue to generate a code until we generate one that doesn't already exist in the database so again very rare that this is going to happen but it's good to plug this in just in case okay so now that we have done this directly on the referral code when the user signs up we want
06:00
to create a referral code now this is going to be incredibly easy we just want another user observer and we just want to create a code out and i'm going to attach that to you to the user so let's go over to our database first tool and just get rid of the codes that we've already got and let's go and create out a user observer so we'll make an observer in here and this is going
06:19
to be a user observer we'll go ahead and hook this up in the same way that we did here so user and user observer and then this user observer once the user is created so we don't want to say creating because that record won't exist in the database and the id won't be able to be filled so once they've been created and all we want to do is say user use that
06:47
referral code method that we have in that trait and just say create now we don't need to pass the user id here because eloquent will hook this up to the model but that's pretty much it now now the reason that we use an observer here it's totally optional is we don't really want to be putting this directly in the controller the registration controller if for example we had
07:05
an admin panel where we could create users this functionality is now global so whenever we create a user it's implicit that we'll always generate a referral code for that user okay so now that we've done this let's go ahead and get rid of the user i created earlier and let's go ahead and sign up again so let's go over and register an account and now when i create this user account we should
07:25
automatically have a code generated there we go and yeah okay user id cannot be null let's see what's happening here and yeah i've said creating i said earlier that we need this to be created so let's go ahead and swap that over and let's try this again so we don't have a user in there that's great let's try this one more time and that just demonstrates why we don't want to do this
07:47
while the user is being created because they don't already exist okay there we go we are registered and of course if we head over to the database we now have a referral code directly attached to that user which is great so now wherever a user is created this is automatically giving them a referral code
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.