تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Eager Loading Relationships0:03

Eloquent makes it very easy to work with related models. But if you're not careful, that convenience can translate into performance issues. Like for example, we have briefly talked about the n plus one problem, I think it was back in the first episode. So that if we wanted to work with relational data, then we need to eagerly load that data. And you know, the n plus one problem is something that we will address in more detail in the next episode. But let's say that we want to display all

that we will address in more detail in the next episode. But let's say that we want to display all of the user's tasks, we're just gonna select the first user and get their tasks. So we would do something like this to where we would get user and we want to include the tasks relationship. And we do that using the width method. We've been using this all throughout this series. And then we will get to the first users so that

We've been using this all throughout this series. And then we will get to the first users so that we could output that. And of course we are going to see the user information as well as their tasks. So we'll go to user tasks and voila, we have the user information and of course all of the tasks that was assigned to the user. And if we wanted to break that down so

that was assigned to the user. And if we wanted to break that down so that we only see the completed tasks, we can do that once again by calling with. But then we need to pass in n Array where the key is the name of the relationship that we want to include. But then we would have a closure that gives us access to the query builder so that we can say where the status is C for completed, in which case that's going

to the query builder so that we can say where the status is C for completed, in which case that's going to select only the completed tasks. And there we have that information and we're not limited to just one relationship. If we also wanted to include the teams that the user is a part of, we could just add that as another element in this array. And not only do we have the completed tasks, but we also have the teams.

Loading Missing Relations2:04

And not only do we have the completed tasks, but we also have the teams. And there's just one team that this user is a part of. And you know, that's all well and good, but what if, you know, we didn't have that to begin with. What if we just have the user so that we get to the first user and then we want to work with, you know, that same information? Well, ideally, yes, we would include that rights whenever we are fetching that user,

Well, ideally, yes, we would include that rights whenever we are fetching that user, but we can include the relational information after the fact of retrieving the user, in which case we call the load missing method. And then we just specify the relationships that we want to load. So in this case, if we wanted to load the tasks and the teams, although we, we need to do that as an array so that we have the tasks

and the teams, although we, we need to do that as an array so that we have the tasks and then the teams, then that is going to load that missing information. So that once again, we see all of the tasks, we see all of the, the teams. But you know what, if we wanted that smaller set of data, maybe we just wanted only the completed tasks. It, it's the same thing. We add a closure that works with the query builder so

It, it's the same thing. We add a closure that works with the query builder so that we can get the tasks where the status is C or whatever status that we are after, in which case it's going to give us the same exact information, except that now it is being done after the fact of getting that first user. Now the beauty about the, the load missing method is that it checks to see if

Now the beauty about the, the load missing method is that it checks to see if that relational data has been already pulled. So if we selected the user and we already included the tasks, then it's not going to load more task information. The tasks are already there. In that case it would just load the teams and then everything would be fine. So it's very efficient as far as how it pulls data,

Counting Related Models4:05

and then everything would be fine. So it's very efficient as far as how it pulls data, because if the relational data is already there, then there's no missing it, it, it's aptly named load missing. And something else that we briefly talked about was getting the counts of something. So let's add another end point, we'll call it tasks count. And really we're gonna do more than just count the tasks, but the idea is going to be the same to

And really we're gonna do more than just count the tasks, but the idea is going to be the same to where we will fetch the first user. But we want, we don't want all of the tasks, we just need to know how many tasks have been assigned to a given user. So in that case, we will call the with count method, we'll pass in the relationship that we want to count, and then we will get that first user so that then we can simply output the user so that we can go to tasks count.

that then we can simply output the user so that we can go to tasks count. And we should see the user information. We don't see any of the task information, but we do see the number of tasks that were assigned, which 10 tasks were assigned to every user. But just like how we could filter that down to get just the completed tasks. We could do the same thing here so that if we only want to count the completed tasks, once again,

We could do the same thing here so that if we only want to count the completed tasks, once again, I, I'm not gonna type that out. I'm going to copy and paste that closure so that it will only include the completed tasks. But you know, we're not limited to one relationship. If we wanted to get the count of the teams that this user is a member of, we can do that as well. In which case, now the tasks count value has changed to two because there's only two completed tasks.

Aggregate Methods Setup5:51

In which case, now the tasks count value has changed to two because there's only two completed tasks. And then there's only one team that this person is a member of. So if you ever want just the count of a relationship, use the with count method, it's a lot more efficient than, you know, fetching all of the related information and then counting it from there. And then finally, we have some aggregate methods. Now actually, before we write any routes here, I want

And then finally, we have some aggregate methods. Now actually, before we write any routes here, I want to create a migration. So we will make a migration to where we will add a duration to tasks. The idea being that we want to track the amount of minutes, let's, let's say minutes that a person spends on a given task. So let's open up add duration to tasks, in which case we are going to add an integer.

So let's open up add duration to tasks, in which case we are going to add an integer. And really this should be an unsigned integer called duration. And we don't really need to do anything with the cedar. We do however, need to open up the task factory so that we can add the duration here. And let's just have a random of one to 100. And then that's PHP Artisan migrate fresh seed so that now we have a duration, in which case

Summing and Averaging7:10

And then that's PHP Artisan migrate fresh seed so that now we have a duration, in which case we can do some pretty neat little things. So let's call this tasks duration. And once again, we are going to fetch the first user, but we're gonna do it like this. We want to sum up all of the durations of the tasks, in which case we'll call the with sum method. We want to include the tasks and we want to sum up the duration values so

We want to include the tasks and we want to sum up the duration values so that then we will get the first user and then we will return that user. So if we go to tasks duration, then we will see that this person has spent a total of 394 minutes. Now notice the name of the fields. Here it is tasks some duration. So we know what this is, it is the sum of that information. But what if, you know,

So we know what this is, it is the sum of that information. But what if, you know, we wanted to add a constraint to that. What if we only wanted the duration or the sum of the durations of all of the active tasks? Well, we would do essentially the same thing of what we've done before, except that we still call this with some. But the first argument is going to be an array where the key is the name of the relationship that we want,

But the first argument is going to be an array where the key is the name of the relationship that we want, and then the value is the closure that has our query builder and we want the active tasks. But then the second argument that we pass to the with sum method is still going to be the column that we want to sum up, in which case now instead of the browser, this should drastically change. We can see that the sum duration is 1 79.

this should drastically change. We can see that the sum duration is 1 79. So these are just the active tasks, but we can do more than just get the sum. We could also get the average with the, with average method, in which case notice the name of the field tasks. Some duration it's gonna change to tasks, average duration. And we can see that the average duration of the tasks are 29.8 minutes. But we can also get the men

are 29.8 minutes. But we can also get the men and the max values, in which case we call the with min or with max, depending upon whichever it is that you want. So because we have this constraint in here, this is going to return the max value of the duration column for any of the active tasks for this given user. So if we take a look at that in the browser, we can see that the max duration was 74 minutes.

So if we take a look at that in the browser, we can see that the max duration was 74 minutes. If we switch this to min, then we will hopefully see something different too. Laravel makes it incredibly easy to work with related data. You can eagerly load relationship information and even filter it down so that you only retrieve the records that you need. Like for example, getting just the completed tasks for a given user.

Like for example, getting just the completed tasks for a given user. It also makes it very easy to load any missing related data. All you have to do is call the load missing method, tell it the relationships that you want to load. And eloquent, we'll load that data for you. But there's also many aggregate functions that you can use to get the counts, the mins, the Maxs, the sums and the averages of your related data. But one of the newer features is really a game changer

and the averages of your related data. But one of the newer features is really a game changer for working with related data. And we will talk about that in the next episode.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟