Playing
02. Creating the referral code schema

Transcript

00:00
OK, so I've created out a fresh Laravel project here with the Laravel Breeze starter kit.
00:05
Let's get started on the referral code schema attaching it to the user. OK, so I'm going to go ahead and register an account first of all. And I'm going to run the default migrations
00:16
that we have with Laravel. I've already set the database up, but if not, this will prompt you. OK, so now that we've got that, we've
00:22
got our users table in here. We can go ahead and just register an account just so we're in there, and then we'll work on this schema. Great.
00:30
So as long as you're at this point, we're good to go. OK, so let's go ahead and make out a model. So we'll make a model called referral code, and we'll create a migration alongside of that as well.
00:43
So let's focus on the migration. So create referral codes table, and let's have a look at what we might need to include here. Now, obviously, these belong to users.
00:53
So the first thing that we're going to need is a foreign ID. So we can identify the user that this is attached to, and we'll go ahead and constrain this as well. So the next thing, and of course really importantly,
01:04
is the code itself. So let's go ahead and just do a string with a code, and we want to make sure this is going to be unique. We are going to be setting up a way
01:12
to make sure when we generate a referral code, it doesn't already exist in the database, but it doesn't hurt to do this at the schema level as well. OK, so the next thing is going to be how many visits we have.
01:22
So we can create an unsigned integer or an unsigned big integer for this, and this is going to be called visits. And what we'll do is set a default on this of 0
01:31
when we actually create this out. Now, the next ones are going to be the clicks. So we want to know how many times this has been clicked. And to be honest, that's pretty much it for here.
01:41
We could store the total earnings, but since we're going to be tallying this up in a table once we get a payout, we could use that instead. But we could add this to that later.
01:52
OK, let's go ahead and run our migrations here. And we're done. Great. So let's focus on the referral code model first of all.
02:01
The first thing I'm going to do is go ahead and set guarded here to just an empty array, just so we can fill things in easily without any fillable fields. And then we want to link this back to a user.
02:12
So let's go ahead and create the user relationship here. And we know that this belongs to a user. So let's say belongs to and our user. And that is pretty much it.
02:23
So when we get an instance of a referral code, when we look this up, when we get our web hooked back by the code, we know who owns this code, which is great.
02:32
OK, so over on the user model, we're going to do things a little bit differently here. What we could do is create the relationship directly in here. But I kind of want all the referral stuff
02:42
to be tucked away and not used directly on this model. So for this, we're going to create out a trait which includes has referrals. Now, there's no need to do this.
02:51
You don't have to do this. But when we're implementing loads of methods on here, this model is going to get really big. So I'm going to go ahead and create out a trait.
02:59
And I'll just do that inside of the models directory and create out a traits folder. And we're going to call this has referrals. So let's go ahead and build this out.
03:08
Let's add in the namespace for this. So that's under models and traits. And this trait is going to be called has referrals. Great.
03:18
So in here, we can now use this trait. So let's say use has referrals. And then we can just add anything we want into this has referrals that relate to referrals.
03:29
We're going to have quite a few things. So let's go ahead and create out the referral codes that this user has. Now, they're only going to have one referral.
03:37
So we're going to use a has one relationship here. So let's go ahead and say this has one and referral code. And that's pretty much it. Great.
03:47
So now what we can do is when a user signs up, we've got access to that relationship. Now, we can actually create this over in the database. So when we get a user signing up,
03:56
we could have an observer in here which is going to go ahead and just fill in all of this information with the code. So let's work on that next.
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!