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
05. Enabling referrals with a PayPal email address

Transcript

00:00
So adding a PayPal email address or whichever email address or thing that you want to pay out to to our profile information doesn't seem that complicated but we need to think
00:09
about this really carefully. So let's say I sign up for an account and I enter my PayPal email address that's going to enable referrals for me. What I then don't want to allow is the user to get rid of that address completely.
00:21
They can update it in the future if they need to but we don't want them to clear this. So we're going to create our new field in here and we're going to adjust the rules based on whether they have referrals enabled or not. Okay so let's start by just adding this field to our profile information.
00:36
Now if we want to find these we're going to go over to views profile partials and we have this update profile information form which gives all of the information for that user. So we're going to grab one of these fields that we already have like the name and we're going to put this just down here underneath and this is going to be our PayPal email address.
00:55
So at the moment obviously it doesn't look great. So just in the meantime I'm going to go ahead and run npm run dev since we're working on some of the client stuff now and let's go ahead and change this over to PayPal. Okay so let's set this to PayPal email and that will be PayPal email and of course this
01:12
can be absolutely anything depending on how you're paying out and let's switch this to PayPal email as well and we'll do the same for this. We'll get rid of autocomplete and I'm going to get rid of required just for now so we can test this out but make sure you add that in there later and we'll adjust the rules
01:31
as well. Okay I think that looks good, great. So now we have a PayPal email address that we can fill in, how do we actually fill this in?
01:38
Well we don't have this within the user model at the moment so let's go ahead and make out a migration here to add the PayPal email to the users table. Okay let's open that migration up so add PayPal email to users table and let's go down and fill this in.
01:53
So we want this to be unique, that's really important. We don't want two users to have the same PayPal email address although you probably could it's not going to cause too many issues but we'll make sure that this is unique and that's pretty much it, great.
02:06
So let's go ahead and run our migrations and we now have a PayPal email. Now just before we do anything let's make sure that we add this to the fillable under our user model or we could just set guarded here completely to an empty array that's probably going to be a little bit easier for the future.
02:24
Okay so now that we've done this we need to know how to actually update this PayPal email address. So we have a profile controller within Breeze and this would probably be different if you're not using Breeze but we have this update section here which fills in with any of the validated
02:38
fields we've given so we've got profile update request here which is responsible for validating this with these rules. So at the moment let's just go ahead and add in a new one in here called PayPal email and this is going to be an email we can keep pretty much all of these in here we can keep this
02:54
rule in here to ignore it if we are updating it later on something else and we should be good. So let's just go ahead and fill this in make sure it updates and then we'll go on to the other rules, great so that's working nicely we now have our PayPal email address.
03:08
Now with a PayPal email we don't want this to be required because when a user updates their profile information and doesn't have a PayPal email we don't want that to be in there so we're going to set this to nullable, let's just head over to the database and get rid of that quickly and let's try this again hit save and yeah PayPal email cannot be null
03:26
so yeah let's make that nullable in our migration so let's say nullable and because I didn't give a down migration I'm just going to go ahead and adjust the structure manually here so we'll say is nullable allowed, yes and we're all good, great so let's just resend that through and we should be good so we can now save that, that's great.
03:51
So now we want to figure out when a user enters their PayPal email address they can change it later but they can't get rid of it completely so at the moment I can go ahead and fill in my PayPal email address but then I can also get rid of it because we've used that nullable rule so how are we going to get around this well over in our rules let's just bring these
04:11
down a little bit so it's a little bit easier to focus we're going to go ahead and add in a required if rule so let's say rule required if and in here we have a condition so any condition that we return in here is going to require this on a specific condition now the condition is that if referrals are already enabled so what does that mean well let's
04:35
go over to our has referrals trait and we'll add this method in here to keep it away so we'll call this referrals enabled and this is going to be handy for later when we build our middleware as well so the conditions here if it's enabled they have a referral code so we need to make sure they actually have a code in the database first of all and
04:55
they have a PayPal email address so as long as they've got a referral code and they've entered their PayPal email address referrals are enabled so let's add these two conditions in here within a return so let's extract this referrals enabled out to another method so or has referral code to another method so let's say has referral code and we can just
05:17
say return this referral code exists although for database that might not be great but we can change that so we'll say if they have a referral code and the PayPal address is not null, not is null and this PayPal email great so they are the two conditions done we've extracted that out to another method which we can use elsewhere if we need to and
05:42
now we have this referrals enabled so here the condition that they are going to be able to update their PayPal email address and it be required is if this user since we're working within a form request we can use this user and we're just going to say referrals enabled great okay let's go over and just try this out I'm going to hit save that work nicely
06:06
so it's only required if referrals are enabled and one of the conditions is that we already have entered a PayPal email address so now when I enter my email address that works I can change it and we'll say Mabel at Cocos.com so that works nicely but when I try and get rid of it now I've technically enabled referrals it doesn't work now if we've got an issue here
06:28
with the name showing with the old data but we can fix that up but that's pretty much it okay so let's go over to our profile information form here and let's just fill in old PayPal email and we should be good so now we've enabled it we just can't get rid of it so it's just not going to update so I'm going to swap that over to Alex at Cocos.com and we're done we can now enter a
06:50
PayPal email address to enable referrals and that's going to allow us to then go on to have a condition of whether we can show the menu in here and whether we can allow the user to access the referral so as long as they've got a PayPal email address they're set up for referrals and they can start accessing that area
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.