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.