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

Introducing hasManyThrough0:00

Next up, we have the hasManyThrough relationship. Not nearly as common as the others, but when you need to reach for it, it's actually fairly useful. So this relationship type allows us to access distant associations. Let's whip up an example to review that together. So we have users and posts from the previous episodes. Let's keep working with that. Maybe you're building a news site, kind of like a glorified blog for politics. And one thing that might be useful is if we made the writers of these articles declare their political affiliation or their leaning.

Creating Affiliation model0:55

Anyways, it just gives us a fun example to work with. So if nothing else, we'll need a table for that. So php artisan, make me a model for the person's political affiliation. I do need a migration for that. And let's also whip up a database factory. Okay, so that gives us our model, our migration, and then a model factory. Let's switch over there. Now to start, I'm just going to say, we'll just have a string column for name, but we could add other relevant fields here if we need to. And that does need to be unique.

could add other relevant fields here if we need to. And that does need to be unique. All right, so I'm going to reset everything. We'll do a fresh run of all the migrations. Let's take a look. Give this a refresh. So we now have users, and we have affiliations, and the users can create posts. So each post is associated with a user. But we did say that the user needs to declare their political leaning. So it sounds like we need to update the user structure to specify this.

Linking Users to affiliations1:47

But we did say that the User needs to declare their political leaning. So it sounds like we need to update the User structure to specify this. We'll say unsigned integer for the affiliation_id. Okay, let's do that one more time and take a look. So now think about it. A User is associated with some kind of political leaning, and that User can now create a Post. Okay, but now what if you had a political leaning, like a conservative, and you wanted to say, give me all of the BlogPost that have been created by conservative writers or by liberal writers. Well how would you do that?

It's a distant relationship. What we would kind of need to do is something like this, if we did it manually. We could say select * from users. I don't think we have any users at the moment. We'll set that up in a moment. But we could say where affiliation_id = 1. So if that's conservative, then we could say, give me all the users who lean in that direction. Now at the moment, we don't have any. So that's what our model factory is for. If I go into database/factories/affiliationFactory.php, this was created automatically when

Updating factories and seeding3:16

So that's what our model factory is for. If I go into database, factories, AffiliationFactory, this was created automatically when we whipped up our model. At the moment, we just have name, right? And let's just say, give me a fake word there. We could even say, make sure it's a unique word. Next, while we're here, I'm going to go into my UserFactory and specify that a User now has an Affiliation. And if you don't give us one, we're going to whip up one on the fly. So create that and then return the ID to me.

And if you don't give us one, we're going to whip up one on the fly. So create that and then return the ID to me. All right, does that all make sense? When we whip up a User, whether for quickly seeding a database or for a test, well, they now need to be associated with some kind of political leaning. So you can give it to us manually. Or if you don't, we're going to create a new Affiliation record, throw it in the database, get that ID column, and then assign it here. Okay, so let's give that a shot. php artisan tinker, please give me a User.

Okay, so let's give that a shot. php artisan tinker, please give me a User. And there we go. Now they're associated with this political affiliation. And it did whip up one on the fly. Now, what if we wanted to override that? So let's get rid of this one. We're going to do it again, but we could just override it. affiliationId is one. Let's do a couple more.

Affiliation ID is one. Let's do a couple more. And then two for liberal leaners. Okay, so anyways, if we get this query a run again, we can see these three people lean to the right, and these two people lean to the left. So at this point, if we were to select the ID, we can see that if we want liberal leaning posts, well, we would look for anything written by these users, 5 and 6. So then I could say select * from posts where user_id in (5, 6). Okay, so we get nothing at the moment because we don't have any posts. Let's whip some of those up.

Defining hasManyThrough on Affiliation5:14

Okay, so we get nothing at the moment because we don't have any Posts. Let's whip some of those up. Factory for a Post, create, but I am going to hard code the User. So we'll say five, there's another one, and then somebody by six. Okay, so if we give that a run, now we can see only the Posts who have been created by these two Users, five and six, and those two Users are affiliated with liberal thinking. Now, it's a little annoying to write all of those queries out, right? So this is where the hasManyThrough relationship comes into play. We're going to go into our Affiliation model, and we're going to say if you have some given Affiliation, like conservative or liberal, and you want to fetch all of the Posts created by

We're going to go into our Affiliation model, and we're going to say if you have some given affiliation, like conservative or liberal, and you want to fetch all of the Posts created by Users who lean in that same direction, we can use a hasManyThrough relationship type. Now, for the arguments, think left to right. Has many what? Has many Posts through what table? The users table. All right, let's try this out, and then we'll break it down together. Give me the liberal affiliation, all right, and now give me all of the Posts associated with that leaning, and we get three.

Give me the liberal affiliation, all right, and now give me all of the Posts associated with that leaning, and we get three. Let's create a new Post now. It will be associated with the User with an ID of one. Let's go back and check. Yeah, that person is associated with the conservative thinking. Associated with the conservative thinking. All right, let's do a couple there, and then maybe this person is as well. So let's give it one more try. Once again, we'll find conservative-minded thinkers, and then give me the Posts associated.

So let's give it one more try. Once again, we'll find conservative-minded thinkers, and then give me the posts associated with them, and here we go. That's all there is to it. So let's break it down. hasManyThrough. So we're trying to say, well, I want to access posts ultimately. I want a collection of posts. However, think about it. For Affiliation, there is no post_id column here that I can access.

However, think about it. For affiliation, there is no post ID column here that I can access. So I have to go through a different relationship. Which one? User. So here's what we're going to do. On User, we're going to look for an affiliation ID right there. Okay, so let's say our affiliation is two here, liberal. All right, well, the first step is find all Users where the affiliation is liberal. Now that we have those Users, five and six, we will take another step.

Inspecting generated SQL queries7:45

All right, well, the first step is find all Users where the affiliation is liberal. Now that we have those Users, five and six, we will take another step. Find all Posts where the user_id is in five or six. That's what's happening behind the scenes. Let's give it a shot together. One more time from scratch. I want to enable the query log. This time, once again, we will find liberal-leaning thinkers. We will fetch all of their Posts. And then let's get the query log.

We will fetch all of their posts. And then let's get the query log. So our first query simply fetches the affiliation. And then here's the main query that does all of the work. So of course, we're grabbing all of the posts because that's what we want. But we're also going to inner join the users table on the condition that the users ID matches the posts user_id column, but also where the affiliation_id matches up. And two is the liberal record in the affiliations table. But once again, SQL, it's confusing. Even once you learn it, it's hard to remember.

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