02. Creating the referral code schema


OK, so I've created out a fresh Laravel project here with the Laravel Breeze starter kit.
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
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
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.
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.
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.
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,
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
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.
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
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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,
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


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.