Fetch Posts in Index 0:00Now, at this point, we have a handful of posts in our database. However, the index page is still showing the hard-coded versions from the Bootstrap template. Let's update that. We're going to come back, go to my Posts controller, and on the index method, I want to fetch all posts. Next, I will pass that through, and a simple compact is fine. So now, this view will have access to a collection of all posts. Let's visit that, and let's see how this is structured. All right, so we have our column, and then we have an individual blog post. So if we come down, yeah, so this gets duplicated over and over. Extract Post Partial 0:32All right, so we have our column, and then we have an individual blog post. So if we come down, yeah, so this gets duplicated over and over. So let's get rid of that, clean this up a bit. Yeah, okay. So now, all of this here, there's a lot of HTML to go through, but it's just paragraph tags. This represents a single blog post. So why don't we grab all of that? And store it in a partial. And this shouldn't go in layouts, because it's unique to posts. So how about post.post, or article, or wrapper, whatever you want.And this shouldn't go in layouts, because it's unique to posts. So how about post.post, or article, or wrapper, whatever you want. How about just post? Okay, resources, views, posts, post, that played. And I will paste that in. Okay, let's just see if that still works. And it does. Okay, so now, we can start making this dynamic. For example, this should be the posts title. And you'll remember, we use this syntax to echo out a field. Loop Posts and Render 1:23For example, this should be the posts title. And you'll remember, we use this syntax to echo out a field. But yeah, if we come back, of course, it's going to fail, right? Because we don't have a post variable. What we sent to this view is a post variable. So why don't we do this? For each post as post, then include this partial. So now, we do have a post variable. And when you call include, think of that as just pasting in all of this data here. So this variable will be available, because it's available there.And when you call include, think of that as just pasting in all of this data here. So this variable will be available, because it's available there. All right, give it a refresh. And now, we've substituted that. Let's do the exact same thing for all of the data. All right, there we go. Post, body. And in our case, it should be two words, right? So it won't look great. But yeah, now, it's all dynamic.So it won't look great. But yeah, now, it's all dynamic. Now, so far, we haven't yet associated a blog post with a user. We're going to cover that in the authentication episode. So I will empty that out for the time being. And now, our remaining step is to make this dynamic. Here's how we can do that. Post, and what do we call? Well, you'll remember when we ran our migration, we have this section for the timestamps. Now, when included, that's going to set up a createdAt and an updatedAt timestamp.Well, you'll remember when we ran our migration, we have this section for the timestamps. Now, when included, that's going to set up a createdAt and an updatedAt timestamp. So why don't we do this? Post, createdAt. But you know what? Let's clean it up real quick. But you know what? It's not going to look that great. So refresh. And that's not what we want. Format Dates with Carbon 2:51So refresh. And that's not what we want. Well, here's a little tip. When you have timestamps, these will automatically be instances of a library called Carbon. The Carbon library is really fantastic. You can see that we can do things like this, createFromDate. Let's even look for things like diffForHumans. This is one of my favorites. So if we have a Carbon instance, you can call diffForHumans. And it's going to format it based upon how far in the future or how far in the past it was.So if we have a Carbon instance, you can call diffForHumans. And it's going to format it based upon how far in the future or how far in the past it was. Let's go to getters. Here's all the various things we can do. Give me the hour, the days in the month, the week of the year. Lots of useful things here. Let's go a little bit further to string formatting. OK, so we can see just find the one you want. So maybe we want this right here. So toFormattedDateString.So maybe we want this right here. So toFormattedDateString. Let's try to use that. Come back, give it a refresh, and now we're good. But next, the title, it's not clickable. So we should, in fact, make that a link. Posts slash the post ID. Like so. All right, give it a refresh. And now those are links. Add Show Route and View 3:58All right, give it a refresh. And now those are links. And of course, style them however you want. But if we click on it, we haven't set up a route for that. So we go to our routes web file and uncomment this from the previous episode. So if we match posts slash an ID, and we do, we load a postController. We load a postController and then a show method. That displays a show view, which is this. So now we should see a place to show the post. OK, let's fix this.So now we should see a place to show the post. OK, let's fix this. First step, let's grab this little section. So we want eight columns here. Next, the H1 will spit out the post itself. And then finally, the body of the post. But now, once again, it's not going to work. We give it a refresh, and there is no post variable. And that's because you didn't pass one to the view. So let's find the post.And that's because you didn't pass one to the view. So let's find the post. We're going to do it the long form way. Like we learned about a while ago. And then we could pass that through. Refresh. And now it works. So the post with an ID of one, two, or three. Or we can use route model binding. Which we learned about.Or we can use route model binding. Which we learned about. Post. And now, don't forget, the wildcard is named post. So that needs to be the variable. Now, Laravel will fetch that for us. And we'll get the exact same thing. All right, so back home, we can view all of these. We can click and switch over to the post. Further, we can create a brand new post. Order Posts by Latest 5:35We can click and switch over to the post. Further, we can create a brand new post. Brand new post. And I'll paste in some lorem text. OK. But now, we're going to see that at the bottom. And we really want that at the top. So it sounds like we need to order them according to when they were last published. We can do that right here. Post.We can do that right here. Post. And I want to fetch the latest ones first. Now, this is just a query scope. Let's take a look. If we dig behind the scenes to Eloquent's builder class, we can see that latest just sets an order by column. So it's the equivalent of saying select star from post, order by, created at, and descending order. Which means I could just as well do this.order by, created at, and descending order. Which means I could just as well do this. Post, order by, created at, and descending order. OK, so refresh. And now that one is at the top. Or ascending order, we'll place it at the bottom, which is the default. So as it turns out, latest, yeah, that's just a bit of sugar, as I would say, to make it a little more convenient for you. Now, as you'll find, real quick, if we switch back, you'll also have oldest. And you'll see that it does the inverse, of course.Now, as you'll find, real quick, if we switch back, you'll also have oldest. And you'll see that it does the inverse, of course. So that can be useful, too. All right, so we're making a lot of progress here. I mean, really, just take a moment and think about everything you've learned. You've learned about views. You've learned about controllers. You've set up migrations. You've set up models here. You've learned about cross-site request forgery.You've set up models here. You've learned about cross-site request forgery. You've learned about controller validation. You've learned about route model binding and views. You've learned how to create data and save it and redirect. You're really doing well. So if you feel overwhelmed, don't. Pat yourself on the back, because you're making great progress.