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.
7 episodes 32 mins

Overview

If you're displaying counts in your app, instead of pulling records into a Collection to count on, try aggregates! Working at the database level, aggregates are performed in one query and lower the memory usage of your app. Let's explore everything you need to know to work effectively with aggregate data in Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!