This episode is for members only

Sign up to access "Laravel Performance" right now.

Get started
Already a member? Sign in to continue
Playing
08. Selecting columns on related models

Transcript

00:00
So we've looked at pulling in only the columns we need for this main model. But what about the relationships that we have as well? Now, user isn't a very data heavy table. But if it was, we would also want to control this as well.
00:16
And then we're going to look at that just now. OK, so once again, I'm going to bump the pagination up to about 300, just so we see a little bit more information in here and we get a higher memory usage. We could even bump that up a little bit further as well if we wanted to.
00:30
So it's not too bad at the moment. We probably won't even see much difference here when we select just from the users because there isn't a lot of data. But again, it's something that you can apply anyway or just as and when you need.
00:44
OK, this line's getting a little bit long. So let's pull in Laravel's specific query method in here and just start to pull these down a little bit, just so we can focus line by line on what we're doing.
00:55
And we're going to look at only selecting the things we want from the user. Now, to do this, what we can do is wrap this in an array, first of all, and we can actually set that as a key and then a function or a closure as the value. What that's going to do is give us a query builder,
01:14
which we can then use to select only the columns that we need. Now, in this case, it's probably just going to be the ID and the name. And that's it. Let's go over, give that a refresh. And sure enough, you can see we've only now selected the ID and the name from the users table.
01:31
Now, sure enough, the memory usage has not gone down too much. The request duration hasn't gone down too much either because we don't have a lot of data in the users table. But if you were pulling in related models that had huge amounts of data like this,
01:45
it makes sense to select only the columns you need, like we did for posts and like we saw a massive memory decrease. Now, there is actually a shortcut to doing this. Sometimes this can get a little bit cumbersome if you having to write this out every single time.
02:00
So there's a really convenient shortcut. I'm going to go ahead and just comment this out just so it's there for you to reference. But you can also say with users or user in this case, and then you can use a colon to define out just the columns you need.
02:13
You'll probably agree that is a lot tidier. And if we just give this a refresh here, you can see this works in exactly the same way. Just bear in mind as well that we do need to pull in the ID. If we only were to pull in the name, we again find ourselves in the situation
02:26
where these two pieces of data can't be joined by the user ID here, this foreign key and this local key just here. So make sure you include the foreign key in your main select and the local key for the user in your sub select just here.
15 episodes1 hr 9 mins

Overview

Let's keep our Laravel applications feeling snappy! In this course, we cover the absolute fundamentals you need to keep in mind when building anything with Laravel.

While Laravel handles a lot for you, it's easy to fall into the trap of not considering and monitoring performance as you go. Keep these tips in your toolbelt, and you'll be able to develop faster apps, from the beginning.

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

Comments

No comments, yet. Be the first to leave a comment.