Planning Following Feature 0:00All right, let's move on to adding friends. Now, we're not writing feature tests for this little project, but let's still try to approach it from the outside in. So if I go to our dashboard, here's our friends list, and right here we're doing a range. Let's swap that out with the user's friends. So how do we want to do this? Do we want to say auth user friends? Maybe, but my only issue with that would be a friend suggests that you follow them and they follow you, right? And that's not necessarily the case.they follow you, right? And that's not necessarily the case. So it's really more who you are following rather than your friend. Or maybe it doesn't matter. I'm not sure. But with that in mind, maybe follows would be the relationship type instead. So for each user that we follow, then render this, and we will grab the user's name, and then finally their email address, which we can replace with user avatar. All right, and then finally, we'll change that to following. So at the moment, we know this is going to fail because I don't have a follows relationship. Defining Follows Relationship 1:02All right, and then finally, we'll change that to following. So at the moment, we know this is going to fail because I don't have a follows relationship. That's our next step. We're going to have a user model, and then right down here, a method called follows. And here's where we need to start thinking of the database structure. So what is the relationship type? Well, a user can follow any number of users. So that sounds to me like a belongs to many relationship. A user can follow many users. All right, let's see if we can build that up. Creating Follows Migration 1:33A user can follow many users. All right, let's see if we can build that up. phpArtisan make a migration called create follows table. All right, let's go there now. All right, and right here, I'm going to swap this out with a snippet to save us some time. And I recommend doing this on your own. So let's see. Let me work this out, and then we'll talk about it. Users, users. Okay, so if we want a relationship between a user and those to which that user follows,Users, users. Okay, so if we want a relationship between a user and those to which that user follows, well, you need two columns. So for example, the user with an ID of one follows the user with an ID of two. Or the user with an ID of one also follows the user with an ID of three. And we set up the necessary constraints. So if you delete the user, let's delete their respective records from this pivot table as well. All right, and one more time, foreign ID is new in Laravel. If you're getting any kind of error, default back to unsigned big integer. Testing Relationship in Tinker 2:24All right, and one more time, foreign ID is new in Laravel. If you're getting any kind of error, default back to unsigned big integer. All right, let's go ahead and migrate the database. php artisan migrate. And there we go. Now, if I go back to my user model, I can find all of the users I currently follow using this relationship. So have a look, let's manually set this up. We have two users. Let's say John follows Montana.We have two users. Let's say John follows Montana. All right, so we'd say, John follows Montana. All right, so now let's see if we're good to go. We might have forgotten something. We'll track down John and then check who he follows. Ah, we do have an exception here. So it's using a convention to figure out what the table name should be. And it thinks it should be user underscore user. But I don't want to do that.And it thinks it should be user underscore user. But I don't want to do that. So instead, I will be explicit that the table name is called follows. And then also because we're using custom column names, I'm going to specify explicitly the foreign pivot key and the related pivot key. So that would be user ID, and then following user ID is what we called it. All right, so now if we put up tinker and give it one more shot, sure enough, John is following this collection of people, and it's only one, Montana. Now if we reverse this, find Montana and see who he or she follows, there's nobody at the moment. Adding Follow Method 3:47Now if we reverse this, find Montana and see who he or she follows, there's nobody at the moment. Okay, so only one remaining step. Let's add a method called follow to create a new relationship. So now we would simply delegate to our relationship and save, and we'll accept the user that we are now following. And I think that should do it. So if I put up tinker, we'll track down Montana there. And we'll say, let's also find John, user find John. Okay, so Montana is going to follow John.And we'll say, let's also find John, user find John. Okay, so Montana is going to follow John. Great. So now if we check everyone that Montana follows, it contains John. We're looking good here. So now if I come back to Firefox and I give it a refresh, it works. John is now following Montana. Let's finish up by creating one more user. So I'll do a factory for this. All right, and actually, let's do one more.So I'll do a factory for this. All right, and actually, let's do one more. Okay, so let's say John is going to follow this new user. Great. Let's now check who John follows. And we can see we now have two different users. And if I give it a refresh, we'll see that represented here. Now of course, at the moment, we are following users through tinker. But in just a bit, when we set up the profile page, we'll add the necessary form to follow any user on the website. Previewing Next Steps 5:11But in just a bit, when we set up the profile page, we'll add the necessary form to follow any user on the website. So now that we have this working, in the next episode, let's update the timeline to include not just John's tweets, but also the tweets of everyone he follows.