Creating Article Model 0:00Here's what we have so far, the homepage as well as an About Us section. But now we're going to focus here. I assume this is articles or press announcements, something like that. Why don't we call it articles? So it sounds like we need a new article and a migration. Alright, let's get started. phpArtisan make a new model called article, and I'll add the dash m flag to also create a migration. Next, I'll quickly visit create articles table, and let's think what it needs.Next, I'll quickly visit create articles table, and let's think what it needs. If I switch back to Firefox, bare minimum it needs a title and a body, but also maybe an excerpt. So why don't we say table, we could do text or a string might be okay here for the title. We'll do a text column for an excerpt, and then finally one more for the body of the post. Now, if we did have multiple authors, we might add a user ID column, Seeding Articles via Tinker 0:56Now, if we did have multiple authors, we might add a user ID column, but for now, I think this is enough. So I will migrate our database, and we now have an articles table as you see right there. Great. Let's now use Eloquent to quickly whip up some articles, and later when we review forms, I'll show you how to do this through the browser. So phpArtisan tinker, and we'll expand, and we'll say, all right, I need a new app article.So phpArtisan tinker, and we'll expand, and we'll say, all right, I need a new app article. Next, we need a title, getting to know us, and then we'll add a short excerpt, and then finally, the full body of the post. There we go. So now, if we take a look at the article, that looks like what we need, so I will go ahead and persist it.that looks like what we need, so I will go ahead and persist it. Now, if I switch back to TablePlus, I'll hit Command R to refresh, and sure enough, I can see that record. Next, to save time, I will do this two more times behind the scenes, and there we go. So if we fast forward, I've created two additional articles. Now, the next step is to render them on the page here. Querying Articles in Routes 1:55So if we fast forward, I've created two additional articles. Now, the next step is to render them on the page here. Okay. So back to phpStorm, and let's start by visiting our routes file, and I think we can stick with the closure a little bit longer. So let's fetch all of our articles. To start, I could say, app article, and you could call all. So let's begin by returning that collection of articlesand you could call all. So let's begin by returning that collection of articles from the routes, and if you remember, if I refresh, we can now review them. And this is okay, but a couple things to keep in mind for future reference. When you call the all method, that is fetching every single record from the table. Now, for a situation like this with a company, they may never have more than a dozen articles,Now, for a situation like this with a company, they may never have more than a dozen articles, so it doesn't really matter. However, if there is a situation where you could have hundreds or even thousands of articles, you don't want to do this. Instead, you might want to take however many you need, and then get the results. So in this case, I'm taking two articles, and refresh, we get that.So in this case, I'm taking two articles, and refresh, we get that. Or you could also paginate them. So I could say, paginate into sets of two. Now, if I refresh, you'll see we have a paginator instance. So again, this is something Laravel provides out of the box to make basic pagination really easy to accomplish. Notice, in this case, because we paginated into sets of two, and we have a total of three articles at the moment, that means we have a total down here of three articles,and we have a total of three articles at the moment, that means we have a total down here of three articles, and the last page is page number two, of course. Anyways, we don't need that in this case, so I will stick with all. But one last thing. Notice that this article at the top was written at, what, 209? And then this one was at 212. Well, traditionally, you want to show your articles in descending order, right?Well, traditionally, you want to show your articles in descending order, right? The newest one should be up top. So we can add the latest method here and then get the results. Latest is just a helper method that updates your SQL query. It's basically the equivalent of saying, order by created at in descending order. Or if you want to order it by the updated at or the published at, you can provide any timestamp you want. Okay, so now if I switch back, notice if I give it a refresh,or the published at, you can provide any timestamp you want. Okay, so now if I switch back, notice if I give it a refresh, this one should be at the top. And it is. Great. And this is likely what you will want. So now we have our articles query. We will next pass the articles to our view. And once again, this is a case where I will often inline it directly to keep it a little bit cleaner. Rendering Articles in View 4:24And once again, this is a case where I will often inline it directly to keep it a little bit cleaner. Next, if we visit our about view in the resources views directory, let's figure out where to display them. If I switch back, let's give it a refresh. You want it right over here in the sidebar. So let's see. Let's find it. Ah, there it is. So here's that sidebar HTML.Ah, there it is. So here's that sidebar HTML. It looks like it has an unordered list. And then we have three list items, and each of those corresponds to one of these. So here's what we'll do. I no longer have to manually create these, so I'll bring it back to one. And then I'm going to use blade. So don't forget, with PHP, you could say for each,And then I'm going to use blade. So don't forget, with PHP, you could say for each, but don't forget we can use blade for all of these common conditionals and checks. So we could say for each articles as article, and then down here let's close it out. Okay. So if we just kept it like this, we would render this list item for however many articles we pulled from the database.we would render this list item for however many articles we pulled from the database. And in this case, it would be three. All right. So now let's just make this text dynamic. Again, with general PHP, you could echo the article's title, but we're going to use blade in almost all situations. Article title. And then here we'll stick with the article's excerpt. All right. Limiting to Recent Articles 5:38And then here we'll stick with the article's excerpt. All right. Let's come back and give it a refresh. And now we have the three articles there. But now here's the thing. If I switch back, let's imagine we duplicate this and we change it a little. Again, however many articles you have will be rendered on the about page, as you see there. But it stands to reason you might just want, for example,as you see there. But it stands to reason you might just want, for example, the three most recent ones. So this is where it is beneficial to be very clear about your query. In this case, I want to take the three most recent articles. And now if I refresh, I only get the latest three articles we've created.