This episode is for members only

Sign up to access "Laravel Aggregates" right now.

Get started
Already a member? Sign in to continue
Playing
04. Multiple aggregates

Transcript

00:00
If we imagine that we're building some sort of user overview with lots of different counts,
00:04
of course we're going to have multiple aggregates. Let's look at how we can do that now. So I'm going to go ahead and just set things up for a little bit later by creating another model and this is going to be a comment. So we're also going to show how many comments a user has. We'll create a migration in a factory here and we'll just keep things simple.
00:22
So let's go ahead and over and create the comments table out and again this is going to be really really simple. Hooking this up to the user with a foreign id and we'll go ahead and constrain that and we'll go ahead and just add in a body here. So let's add some text and let's say body. Okay let's go ahead and run phpArtisan migrate
00:42
and let's go over to the comment factory and just generate out a fake body. So body is just going to be this faker and sentence and let's do it a little bit larger at 10. Okay so now we can go ahead and say phpArtisan tinker app models and comment. Grab the factory go ahead and do this say 50 times it doesn't really matter and we'll create that out and
01:09
pass the user id in which we know is one. There we go so we now have 50 or so comments on here. Right so now let's go over to our routes and web and go ahead and add in another aggregate to this. Now for now we'll just die dump this just so we don't need to output anything and let's see how we can do this. Now we've already got from the last episode this callback
01:33
in here inside of this array or this function assigned to this value which will go ahead and constrain this. Now what we can do is do everything inside of this array so we can just add in an extra comment or if you weren't constraining this you could just pass this in as an array or two items like this. So let's just take a look at this very simple form first of all give this
01:57
a refresh and yeah we don't actually have the relationship set up that would help so let's just copy and paste this down and let's change this to comments and of course change the model here to comment. Okay give that a refresh and let's have a look there we go we've got articles count and we've got comments count so it's most basic that is how easy multiple aggregates are. If we
02:22
bring this back to how we had it before we can also kind of mix and match this so we can have a constraint on the articles but not a constraint on the comments and if we just give this a refresh you can see this still works we have two published articles but we have a total of 50 comments with absolutely no constraint on them. That's not to stop you from going ahead and adding a constraint
02:45
on your comments as well so maybe these were not published or they'd been hidden or something like that. I'll let you go ahead and do that depending on what you're building but there we go that is our multiple aggregates including mixing and matching these with the conditions that we've seen here before.

Episode summary

In this episode, we're diving into how to work with multiple aggregates in our user overview feature. We start by imagining a dashboard where we want to show not just one, but several different counts for a user—like the number of articles they've written, plus how many comments they've left.

To set this up, we first create a new Comment model, its migration, and a factory to make generating fake comment data easy. After running our migration, we use the factory (with Tinker) to quickly whip up a bunch of comments, all attached to the same user. This way, we have some realistic data to work with as we add new aggregates.

Next, we go into our routes and set up the logic to count both articles and comments for a user. You'll see how easy it is to add more aggregates—just toss more entries into the array! We make sure to set up the right relationships in our models, so everything connects smoothly.

Finally, we play around with conditions: maybe you only want to count articles that are published, but count all comments (or maybe you want to add filters to comments too). You can totally mix and match these constraints as needed. It's all about showing you how flexible these aggregate queries can be, and setting you up to handle whatever counting needs your app throws at you.

Episode discussion

No comments, yet. Be the first!