This episode is for members only

Sign up to access "Build a File Marketplace with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
33. Calculating and displaying download stats

Episodes

0%
Your progress
  • Total: 3h 32m
  • Played: 0m
  • Remaining: 3h 32m
Join or sign in to track your progress

Transcript

00:00
We're going to finish up with some sales stats on our dashboard and a list of the sales that we've made on the platform. So the first thing that we're going to do is head over to the dashboard page and just really quickly design out a stats section inside of here.
00:16
So we're going to get rid of this white container and we're going to go ahead and create a div out in here which will house this. Let's create an H3 out in here and say sales stats and we'll just style this up really quickly with a few styles. So let's say font semi-bold, we'll set the text here to maybe like a grey 900 colour and we have something like that and the stats of course are going to fit underneath here. So we're going to create a definition list out here, inside of each of these are going to be a div and then for each of these we're going to have each of the stats.
00:52
So we'll just create two of these here now, we'll style one out and then we'll copy it over. So for the overall definition list here, let's set a margin on the top here to space this out. We want this styled up with grids, which we've already covered in the course and we want the colour set to two here because we're just going to be showing two stats and we'll have a gap of six inside of here. Now for each of these, we're going to go ahead and set a background of white on this.
01:16
We'll go ahead and set an overflow, a small shadow on this and then for a small viewport, we'll go up and round it to large. We'll set a padding on the x-axis to four, a padding on the y-axis to five and then for a small viewport up, we'll set a padding to six. So that is going to be one of our stats just in there and then of course, we're going to have one sitting next to this as well. So inside of here, let's create out the head of this, which is going to be sales and we'll style this up with small text and we'll set the text here to a grey 500.
01:54
So that's going to look like this and then for the DD here, let's go ahead and put in a sales count. Let's style this up really quickly. We'll set a margin top of one here. We'll set the text pretty big because this is what we want to see here.
02:10
So let's say 3XL, we'll set the font to semi bold and we'll set the text to a grey 900 to match everything else. OK, so we have the following sales 100. That's what we want to see for two of these. So we can go ahead and take this, duplicate this down here and we can change this over.
02:29
So the next is going to be the sales volume and that will be how many products we've sold. There we go. So there are two stats. Let's head over to our dashboard controller and go ahead and calculate these out in the fastest way we possibly can.
02:42
To do this, what we're going to do is actually take in the user from the request and we're going to preload at the database level the sum of the sales and the price. Then we're going to load the count of the sales. This just saves us having to do something like request user sales and then count and then loading all these in just to count on them. Much more efficient to do this at the database level.
03:07
So we're going to say request user. We're going to go ahead and say load count and we're going to load the sales count. What that will do if we just die dump on the user here and we head over to our dashboard. Well, we don't actually have the sales relationship here.
03:24
So let's go ahead over to our user. And we've kind of got a problem here, because if we think about our database set up for each of the sales, we only have a product which then belongs to a user. So the relationship that we're going to have to set up in here needs to go through the products table to fetch each of the sales. So let's go ahead and define the relationship out in here called sales.
03:49
And we're going to use a slightly different and slightly more advanced Laravel relationship here, and that is has many through. Now, for this, what we do is we pass in what we are trying to grab. In this case, it's the sale. And we're going to pass in what we're grabbing this through.
04:05
So a user has many products, a product contains many sales. We can actually grab the sales via the product. So now if we come over, that works. If we go ahead and check out the attributes for this user now, you can see this now contains a sales count on here.
04:22
And that means that's been done at the database level and applied to our model. So we now have the sales count in here without manually having to calculate that. Now, the next thing that we're going to do is use load sum. And we're going to go ahead and load up the sales price.
04:40
So each of the sales that a user has received, we're going to sum up the price that they were sold at. Remember that we were storing this at the price that it was actually sold at, not necessarily the current price that we have. Again, let's go ahead and just die dump on the user in here just to see. And again, that's happening at the database level.
05:02
What we should now have is the sales sum price attribute. So we can now use these values directly over on our dashboard to show these out. So let's head over to our dashboard.blade.php file. And we can just simply replace this information out.
05:21
So we're going to say auth user and sales count. Let's head over, give that a refresh. We've got four. And we can do exactly the same thing here for the sales volume.
05:33
And we know that sales and sum price. OK, let's give that a refresh. And there we go. Now, obviously, this is in cents and it doesn't look great at the moment.
05:46
The money package that we pulled in earlier can very easily format directly within Blade files by using the helper. We've already seen this, that when we output this, it will automatically cast this to a formatted amount with a currency symbol. So there we go. We've got our sales count and the total amount that we've processed in sales.

Episode summary

In this episode, we're wrapping up the dashboard for our course project by adding a section that displays key sales stats. We start by tweaking the layout—removing a container and designing a neat area using a definition list that will show stats like the number of sales and the total sales volume. There's a quick little CSS refresher with grids, colors, and padding to make our stats look presentable.

Next up, we jump into making these stats dynamic. Instead of loading all the sales into memory, we optimize our queries using Laravel's loadCount and loadSum methods right from the user model, which lets us pull the sales count and total sales value straight from the database without extra overhead. But for this to work, we realize our user needs a relationship to sales, and because sales are linked through products, we set up a hasManyThrough relationship—slightly advanced, but super useful!

Once we've got our data, we simply update the dashboard to show the stats, and even make use of our money formatting package to display the sales volume as a nicely formatted currency amount. By the end, our dashboard will clearly display how many sales have been made and their total value, looking smart and professional.

Episode discussion

No comments, yet. Be the first!