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
24. Showing historical payouts for users

Transcript

00:00
OK, so now that we've generated that CSV and we've marked these payments as paid, we can show historical payouts and sum all this up,
00:07
but on our dashboard so we can see what we've made over the months. So the first thing that we're going to do is head over to the referral dashboard controller,
00:16
and we're just going to output this data in here as payouts. So we're going to do a very similar thing to what we did when we were generating the CSV, but this time we want to group this by the month.
00:26
So let's go and grab the currently authenticated user here. We'll go ahead and say referral payments. Let's just make sure we included that in here.
00:35
Yep, so referral payments, and we will start to build this up. So we want to say we're not null on the paid at date because, of course, this needs to have been paid out, which
00:47
is really important. And we want to go ahead and select the paid at column, which is what we want to see when it's paid. Then we want to select on a raw because we
01:00
want to sum up the amount kind of like we did, well, exactly like we did when we generated the CSV, and we're going to call that amount. And then we want to group this, but this time we
01:10
are grouping by the paid at date. And that is why we included that as a date and not a timestamp as well because if it was a timestamp and it was paid out at different times of the day,
01:20
we're not going to be able to group this properly. OK, so finally we'll just say get, and we should be good. So let's head over and give that a refresh, and we will just start to output this data.
01:30
So if we come over, let's look at what's similar here. I think active referrals is probably the similar one. So let's open up our referrals under subscriptions. We'll copy this, and we'll create out a new one.
01:44
So let's say referral payouts.blade.php, paste this in. Let's change the title. So, of course, we'll just call that payouts or something. And again, I'm just going to copy over some text
01:56
that I used earlier for this, so your payouts from referrals. OK, so what kind of data do we need here? Well, we just want the date and the amount. You can include more later if you want to.
02:07
So let's just call this date and amount. And then we want to iterate through the payouts as payout, and we only need one of these. So we want the date in here, and we want the amount in here,
02:23
like so. OK, so we should be good to just include this directly over on our index. So let's grab this, and we'll say referral payouts.
02:36
And there we go. Great. So we know that we've already got one payout here. Mabel has one payout.
02:42
Alex has one payout. But now we can just fill in the data here. OK, so the date is just going to be the paid out date to time string.
02:51
This isn't going to work at the moment, and I'll show you why. So let's say payout paid at to date string. And if we give that a refresh, we get an error. And that is just because on the referral payout model,
03:07
we're not casting this to a timestamp. So let's set paid at to a date time, or just a date, and give that a refresh. And there we go.
03:16
So that is when that was paid out, which is today because I obviously run the command today. Now, the amount is, again, pretty straightforward
03:24
because we've already looked at this. We just need to output the amount. We know that that is cast to a money object. So when we output it in Blade, it's
03:32
going to format it for us with the currency symbol. So let's say payout amount. And there we go. We know that that was $60.
03:42
So there we go. We are now listing all historical payouts. And of course, if we had more, that is going to work. And we can kind of demonstrate this
03:50
by going over to our referral payments here. And we know that we've got these two as user 2, but we can switch them to 1. And we can change the date over to something else.
04:02
So let's just switch the date over there, save that out, and there we go. We just simulated another payout. So there we go.
04:10
We're now listing all of the payouts that happen when that command is run to give us the CSV to pay out, hopefully on the same day, and the user can see what they've earned over time.

Episode summary

In this episode, we work on displaying a history of payouts for users on their dashboard. Now that we've set up generating a CSV for payments and marked them as paid, it's time to actually show users what they've earned over time.

We'll start by updating the referral dashboard controller to gather all the user's paid referral payments and group them by month. This involves grabbing just the ones that have actually been paid (based on the paid_at column), summing up the amounts, and grouping by the date. This lets us see monthly earnings at a glance.

Then, we set up a fresh Blade view to show this payouts data. We copy over some structure from another section in the app, update it to focus on payouts, and display just the date and amount for each payout. A bit of tweaking is needed—casting the paid date properly and formatting the amount—but once that's done, historical payouts appear neatly on the dashboard, formatted as expected.

Finally, we simulate some different payout scenarios by editing the data directly and see how the listing updates, confirming it all works. So now, whenever payouts are made, users can easily check their dashboard and see what they've received in the past!

Episode discussion

No comments, yet. Be the first!