Move Timeline to Tweets 0:00We are now ready to update our timeline. John Doe is following two people, but we aren't yet seeing their tweets in the timeline. Alright, let's get started. But actually, real quick, in my routes file, notice that our home controller is actually the one that's displaying the timeline. But then I also have a tweets controller. It's kind of confusing, right? It should be the tweets controller that displays the timeline. Okay, so with that in mind, I'm going to add a new one here. If you make a visit to, uh, or if you visit slash tweets, that will hit tweets controller at index. Okay, so now I'm going to go to home controller. I'm going to grab this thing right here, and I'm going to move it to tweets controller. Now, if I switch back to home controller, I don't evengoing to go to home controller. I'm going to grab this thing right here, and I'm going to move it to tweets controller. Now, if I switch back to home controller, I don't even think I need this anymore. So I'm going to delete it entirely. And then lastly, if I go back to my routes file, a couple of things. First, this is still effectively our home, so I will move over that name. Next though, what about when you log in? Well, in my login controller, by default, Laravel will redirect to this string. And if you take a look at it, that's actually a hard-coded path. Let's update it. Now, when you sign in, you'll be taken to the correct page. And we won't test that out, but I assure you it works. All right. Finally, last thing. Now we no longer have any protection in place. So even if I'm a guest, check this out, it tries to load that page and it can't. It's trying to call timeline Add Auth Protection 1:24Finally, last thing. Now we no longer have any protection in place. So even if I'm a guest, check this out, it tries to load that page and it can't. It's trying to call timeline on a null user. We can't do that. So of course we could add a route to grouping here if you prefer. You could alternatively add your auth middleware in the controller. Again, highly skilled developers like both approaches. It's up to you. Route, apply the auth middleware to this group. All right. So now if I switch back to Firefox and refresh, I will be redirected to the login page. And we need to do a little CSS work there, but that's okay. Otherwise, if you are signed in, you're good to go. All right. I'm ready to go. So now update the timeline. Well, go to our tweets controller. We create their timeline by calling this method. So let's take a look at that. At the moment, our timeline exclusively consists of tweets created by Add User Tweets Relation 2:15the timeline. Well, go to our tweets controller. We create their timeline by calling this method. So let's take a look at that. At the moment, our timeline exclusively consists of tweets created by the current user. So here's what I'm thinking. There very much is a point when we will want only the current user's tweets. So with that in mind, why don't we add a new relationship here called tweets? We already know that a user can have many tweets. So this user has created these tweets. Okay. Well, that sounds like a has many relationship. Like so. So now if we were to boot up Tinker and we find, is this John? Yes, it is. And we grab his tweets using that new relationship, we get only his tweets and we will need that for his profile page. Okay. But next for the timeline, yeah, we need to tweak this a little bit. So what do we want to include? Include all of the user's tweets as well as the tweets of everyone they Build Followed Tweets Query 3:06profile page. Okay. But next for the timeline, yeah, we need to tweak this a little bit. So what do we want to include? Include all of the user's tweets as well as the tweets of everyone they follow or he or she follows. Finally, we want it in descending order by date. That's what we need here. So why don't we play around? Because if you think about it, we want to fetch all of the tweets where the user ID is somebody John follows or John himself. Now we already have this follows relationship, right? So if we were to play around PHP artisan Tinker, let's find John. I think he's the first. Yeah. So yeah, if I were to say John follows, we're going to grab all of the users that he follows. And if I wanted to grab the ID, now we have the IDs of the users he follows. Okay, so why don't we grab that and start building this up? We could say IDs equals this, and then I'll paste that in. Alright, so now we have the user IDs. Next, though, we want John's as well. So weso why don't we grab that and start building this up? We could say IDs equals this, and then I'll paste that in. Alright, so now we have the user IDs. Next, though, we want John's as well. So we have a collection here. Let's push John's ID as well. Okay, now we have a collection of user IDs. Why don't we finish up by saying, give me all of the tweets where the user ID is in this array that we've created, sort them in descending order, and give me the results. And let's try that out. So we come back to Firefox, we give it a refresh. And I think we're good to go. So now notice, in addition to John's tweets, he also sees Montana's and Juliet's. Why don't we simulate Juliet creating a new tweet. So her ID is five. Let's say factory for a tweet, create one, but set the user ID to five. Alright, now back in Firefox, we give it a refresh. And that will jump to the top of his timeline. So that's a fairly simple way to go about it. But just one Optimize IDs Retrieval 5:00but set the user ID to five. Alright, now back in Firefox, we give it a refresh. And that will jump to the top of his timeline. So that's a fairly simple way to go about it. But just one quick thing here. Notice I call follows like this, that's actually going to load everyone, the current user follows. So you can imagine situations where a person's following 5,000 people, this right here would create a collection of 5,000 users. So why don't we instead chain it like this. So we're only getting the IDs and not a full collection of users, at which point we then pluck their IDs. This will be more performant. Next, I think this is fine. But if you want, we could call this friends. And then we could alternatively say, give me any tweets from our friends or where the user ID is the current user. So we could just do it in line there. Anyways, there's some other relationship specific ways we could go about it. But I think this will be fine.friends or where the user ID is the current user. So we could just do it in line there. Anyways, there's some other relationship specific ways we could go about it. But I think this will be fine. So with that complete the next episode, we're going to work on a user's profile. And this is what it'll look like.