This episode is for members only

Sign up to access "Laravel Aggregates" right now.

Get started
Already a member? Sign in to continue
Playing
02. Aggregate aliasing

Transcript

00:00
So we saw earlier for this aggregate in particular that when we get that property out in the user model that gives us articles underscore count.
00:11
Now you might want to alias this at some point and any of the aggregates that we create within Laravel, whether they're counting or summing or any of the other stuff that we're going to look at in this series, we can actually alias these. So to do this we say articles and then as and then we give the alias name just here.
00:29
So for example articles as articles count and I can't think of a good name so I'm just going to call this articles count alias. So now when we come over instead of articles count we're going to have articles count alias. Let's go over give this a refresh and there we go. We get exactly the same thing.
00:47
You can see here in the query we're saying as at the end articles count alias. So Laravel will just take that, put it into the query as per that syntax that we've just used within the with count method and it will go ahead and alias this for you. So just bear this in mind through the rest of the series that you can alias any of your aggregates.
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!