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
04. Retrospectively assigning referral codes

Transcript

00:00
So this is totally optional, but if you are retrospectively
00:02
building this on top of users that you already have, you're going to want to go back and give your existing users referral codes. And this is how you would do it.
00:12
So let's go over and generate out a command. So we'll make out a console command. And let's call this assign referral code to users. So let's go ahead and build this out.
00:24
That will be put in the console commands directory. So assign referral code to users. It's already given us a name for this, so we'll just leave that as the default.
00:33
And in here, we want to go through and potentially update thousands of users that you already have. Okay, so how are we going to do this? Let's just run this command first of all,
00:42
just so we know that we've got this. So app assign referral code to users, and of course it doesn't do anything at the moment. Okay, so what do we want as the condition for this?
00:52
Well, we want to know that they don't have a referral code already because we don't want to generate another referral code and replace their existing code. So really importantly, we're going to say doesn't have,
01:02
and we're going to use the name of that relationship to make sure they don't already have that relationship in the database. So if they don't already have a referral code,
01:10
what do we want to do? Well, we don't just want to start to iterate through them because if you potentially have thousands of records, this is going to load all of these users into memory,
01:17
and that's not going to be great. It will probably crash. So we're going to go ahead and chunk these by 100 records per iteration.
01:25
So this will grab 100 users, and then it will go on to the next 100 chunks. So it won't load everything into memory in one go. Now, inside of this closure that we have here,
01:35
we will now get a collection of users through, and then for each of them chunks, we can then iterate over and create these. Now, there are more efficient ways of doing this.
01:44
We're technically iterating over 100 users and creating 100 queries all in one go, but since this is just going to be a one-off command, it doesn't really matter too much.
01:53
So we're going to say users each, so that is now a collection, so we can use that nice each method, and then into this, we will now get the individual users.
02:02
So inside of here, we now just want to say user referral code and create much like we did with our observer. So this is now a one-off command that we can use to generate referral codes
02:13
for all of the existing users in our system. So let's just go ahead and run this and just see what happens. Of course, nothing happens.
02:19
If we check the database, this user already had a referral code, so that's not happened. Let's test this on a bigger scale.
02:26
To do this, we'll go over to Tynker and let's generate out a bunch of users. So let's say user create, and I'm actually going to get rid of this user to test this,
02:39
and let's create or use our factory more appropriately. Let's create out 10,000 users. There we go, and let's just wait for that to finish because that's going to take a while, and there we go.
02:52
Great, so if we head over to the database now, we've got 10,000 users and we've got 10,000 referral codes. Now that happened because we're manually creating these users, but let's say we had,
03:03
well, we'll truncate this table. Let's say that we had 10,000 users already in there without a referral code. Now we can go ahead and run our command.
03:13
So let's just quit out of here and let's go ahead and rerun that command here. So we'll wait for that to finish. It's not going to crash
03:21
because we're chunking these users by 100, and that was pretty quick, and as you can see, a referral code has now been created for each of them users. So there we go.
03:30
Keep this command handy if you are retrospectively going back and updating the users you've already got, and there we go. We've created referral codes for all of our existing users.
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.