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
16. Showing referral stats

Transcript

00:00
Okay so now we've got all of this data rolling in let's go ahead and update our stats so we can show
00:05
the clicks the conversions and all of the subscriptions that have been created from our referral code. Okay so if we come over to our dashboard just here we just take this container and pull it down we should end up with the following. Great so what we can do now is just create our new partial for our referral stats and we should be good. So over in our partials
00:29
let's create referral and let's go ahead and just grab the structure of this and paste it over to here but of course just get rid of everything we don't need down here. So let's get rid of this entire div and let's just create a new one out and we'll just say stats. Okay so these are our referral stats and again I'm just going to copy over some text that I wrote earlier. Okay there
00:55
we go referral stats here's how you're doing. So with our stats let's just add a margin on the top of six the same as before and let's start to output these. Okay we'll build the table out first of all that kind of makes sense so we'll create out a table in here and we'll have a minimum width of full we'll create a t-body for this and we'll have a table row for each of these
01:18
so let's pull that in properly and we want the first one to be visit so how many times this was visited so let's create out a td with a class in here that's going to be the header and then we're going to have a td underneath that that's going to be how many visits we got so let's just style this up really quickly at the moment it's probably not going to look great so for this let's say font
01:41
medium since it's kind of like a header we'll set some padding on this and we'll set the padding left to zero so it doesn't look too far over from the left we'll set a padding on the right of three and we don't actually need to do that and we'll set a text to left and we'll do pretty much the same thing for the value just here but we'll get rid of font medium okay let's check that out and
02:05
that looks a lot better great okay so we'll have a table row for each of these stats that we want to show so we know we've got visits we've got clicks and we have got conversions so how many subscriptions have been pulled over from this okay so let's finish up some of the styling for this body really quickly so we're going to go ahead and say divide y that will just create a
02:31
divider on the y-axis and then we can set the color to this as well so let's say gray 200 let's have a look and there we go okay so if we head over to our referral dashboard controller we're not passing anything down at the moment and we're going to need to pass down our actual referral code that's pretty easy we can either just do auth user and referral code like so or
02:57
of course we can grab it from the request it doesn't really matter too much so now that we've got access to that referral code what we can do is just start to output the basics so we're going to say referral code visits we know that that is just a number in the database and we've got 10 which is great and we'll do the same thing down here for the clicks which should be a little bit
03:20
less and that's three now for the conversions that is how many subscriptions we have created via this intermediate relation here with the referral code subscription so that's how many people have converted and actually created a subscription here and if we head over to the referral code model we know that we've got that subscriptions relation in there so we could just
03:40
output a really simple count here we could do a sum on this at the database level but let's just go ahead and say referral code subscriptions and we'll just say count so we'll just keep things really simple and of course we know that we've had one conversion great okay so now that we've done that what we can equally do is create another one of these so we can duplicate this down and have
04:05
referral subscriptions and that will list out all of the subscriptions that we have from that and it will allow us to calculate out how much we're going to give that user and all that kind of stuff so for the referral subscriptions let's just take the stats because that's going to be pretty similar and we'll create a referral subscriptions in here paste all that in we'll
04:29
just leave that in as it is at the moment and let's just change over in here so we're going to call these active referrals and this is important because these are the referrals that haven't cancelled their subscription so again i'm just going to pull over some text that i have from my notes here and there we go so these are currently active referrals that we're receiving payouts for
04:53
okay so the structure of this is going to be slightly different we can keep pretty much all of the styles in here but let's get rid of these two other rows because we're going to iterate through to create more rows so what we're going to do is now bring in a t head and we're going to have a row in here and these are going to be the headers so let's create out some table headers
05:15
this is going to be when they signed up then we're going to have the plan that they are currently on and then we're going to have the percentage that we're receiving with that amount so your percentage okay so at the moment this isn't going to look great but let's just add in a fake date in here let's add in a fake plan and then finally we'll just add in a fake value so let's just say twenty
05:38
percent and then in brackets we kind of want to give the value for this so in our case that's going to be twenty dollars for a hundred dollar subscription okay let's get rid of this font medium here just have a look at this and yeah at the moment sure enough doesn't look great let's style up these headers so we can actually take the styles from here these are going to
05:56
look pretty similar so let's add in class for each of these and let's just take a look and there we go great that looks fine all right so we want to iterate through these now so we want to actually pass these down first of all from our controller and let's do this the easy way first by just accessing them directly on the referral code so let's go down to our referral subscriptions and
06:20
just start to iterate through them and then i'll explain what the problem is so let's go ahead and do a for each on our referral code subscriptions as a subscription and let's end that for each down there and then in here we should now have one showing great so let's output the date of this subscription so we'll say subscription created at and let's say to date string and there we go so
06:51
we actually get that now for the plan this is slightly different if we just open up the subscription model within cashier this doesn't link up to a plan of course because our plans are our own thing so what we're going to need to do in a to get the plan that this user is currently on is move over the cashier model to be something different so that will be our own one that we
07:15
extend and add more functionality onto so let's do that now just so we can continue with the rest not let anything else get in our way okay so what we're going to do is head over to our app service provider and in here what we can do is tell stripe or cashier sorry which model we're using for a subscription so to do this we'll do this under boot and we can say cashier let's pull in the facade
07:37
here or the class here new use subscription model and then we can give our own model now we don't have one at the moment so let's go and create out a model called subscription we're not going to create a migration for that of course because we already have that stuff in the database but now we can hook that up to our own subscription model and then really importantly with that
08:03
subscription model this needs to extend the base subscription model from cashier so we'll just extend that we can get rid of use has factory so we don't really need to use that in there and that should be good so now what we can do this should still just work so everything still works but now what we can do in our own subscription model is create out a relationship
08:24
to the plan from that subscription so to do this let's think about it we'll go ahead and create the plan relationship out and it belongs to a plan in our own database but for the columns to hook up to because this isn't quite going to work if we just go over and say subscription plan and let's just have a look at what we call these so title that's not quite going to hook up because we have
08:57
a null value so what we need to do is figure out how to link these up with the stripe price in the subscriptions table that's been created from stripe and the stripe price now own plans table so if we look at this we've got our subscriptions here this contains the stripe price which is of course going to be the same as the plan stripe price over here so basically these just two
09:21
columns need to map up so we're going to say stripe price for the foreign key and stripe price for the owner key so we're telling eloquent how to map these up so now what we get is monthly so this value now is actually coming from that plan which is great so a little bit of a diversion there but we need to do that so we can hook up these subscriptions to the plans and see what plan we're
09:45
on and this is useful for just general functionality as well for telling a user which plan they're on okay so for the percentage this is pretty easy what i'm going to do is just get rid of this and we're going to go and say subscription and pivot so we know that when we linked up the subscription to the referral code we included this pivot this multiplier and then if we go over
10:10
to our referral code model we know that we made sure to include that so what we should see over on here is the multiplier great so we could what we can do is we can use that to just output the percentage so let's grab the multiplier and of course we don't want to show 0.2 we just want to multiply that by 100 and that gives us 20 the actual percentage and then of course we can just
10:36
add a percentage sign onto there great so we've got the percentage now we need to work out the actual percentage of the plans price that we are getting so let's go ahead and add in them brackets again and get this value so what we want to do is say subscription plan price and we'll actually output this like so now the plan remember has this cast so the object we get back here is a
11:05
money object and what we can do with that money object is use some methods like multiply or add whatever we want to do so if we multiply the plans price by the multiplier that's going to give us the actual amount so let's go and have a look and there we go and because we're using that money object when we dump this in blade and output it to the page it's going to be formatted as a value for us
11:30
okay great so now we are showing not only our referral stats but also our active referrals and we've not had to do too much extra work to get the plan to show or the price to show okay there we go great so that is all of our stats that we've got so far we now need to focus on what happens in the future when another payment rolls in what do we do with it
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.