Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

5.1 Displaying the Post List

In this lesson, we're going to start writing our blog management, which is going to be very similar to everything else that we have done. Because for the most part, it's just basic CRUD. So we're just going to fly through this and we're going to start by creating our model, our migrations, our table seeder, and our blog controller, and all of that stuff. So let's just jump right in, php artisan make:model. We'll call this Post. We'll say -m to create our migration so that we can start working with the database stuff. And, that was rather quick. It's usually taking a few seconds, actually, more than a few seconds for that to complete, but that's fine, I'm not going to complain. Next up, we want to create our seeder. So artisan make:seeder, and we'll call that PostsTableSeeder. And it always helps to type artisan correctly, artisan, there we go. We will have our seeder there. Next is our controller. Now I'm going to call this blog controller instead of a posts controller. And we need to remember to say Admin/BlogController. This will be a resource controller. And let's go ahead and set the model=Post, because that's primarily what we're going to be working with, and there we go. So let's get into our code editor. Let's start with our database migration. So database migrations, and it's not there. So let's refresh, there it is. And I'm just going to paste this in. So we want essentially the same stuff that we had inside of our pages. However, there's a few other things. The user_id, we could use author_id. But since this is linking to our users table, I want to say that user_id, because that is significant. User is linking to the users table, id is, of course, the primary key of that table. So user_id, that's how we will link the user with their posts. We have the title, we have the slug, which isn't the URL, but it's going to be part of the URL. Then we have the body and an excerpt. But then here's this published_at. It's a timestamp, and notice that it is nullable. So we don't have to have a flag to check to see if a blog post has been published or not. If published_at is null, it hasn't been published. If it has a value, then it has been published. So we have that. Let's go ahead and create our seeder, well, populate our seeder. So let's go to PostsTableSeeder. And once again, I'm going to cheat and paste this stuff in. So it's a lot like what we have done for everything else. The first thing we do is truncate our Post table. And then in this case, we are once again using our admin user and we are creating a bunch of posts and saving them as posts for that user, although we do need to specify an excerpt, don't we? So we will say excerpt and Blog Post 1 Excerpt, for the lack of anything better. Let's copy that, paste it a few times so that each one of these posts have it. And we will just change the numbers, so that's Post 2, then Post 3, and there we go. Let's also add this to our database seeder just so that if we ever run just db seed, it will be included. So we will say PostsTableSeeder::class. And that will be good there. And I think that's it. Let's go ahead and perform that migration. So php artisan migrate, that will create that table. And then we will say php artisan db:seed, and we will specify our class, and that was PostsTableSeeder. So users not found, that's a problem. We need to add the use statements here. So we will say, use App\User. And we want to do the same thing for Post as well. So let's go back, run the command. We should have data there. So now let's go to our controller. Let's close everything else. And inside of our controller, we're going to do a lot of the same stuff. But before we do that, I guess we should do something for our views. Let's use our pages as a basis once again, especially in this case. Because between creating and editing, we have that partial view that we didn't really use whenever we did it with users. Well, now we will. So we have that as a starting point. We'll just call that folder blog. And so inside of our blog controller, inside of Admin BlogController, let's do this for the index method. Actually, let's just open up pages. Let's start copying and pasting. Cuz I've repeated this, but I'm going to say it again, a lot of this is going to be the same. We do need to set up our model, though, don't we? Because we need to be able to link a user with his or her posts. So let's open up Page, let's open up Post, and let's open up User. We would do the same thing. So public function posts, and then we will return hasMany('App\Post'), and then we need the reverse. So we will just take what we did inside of Page, and we will paste that inside of Post. And we can also go ahead and set the fillable. So we can do that so that we don't run into any mass assignment issues later on. So we have title, we have slug, we have body. There's also the excerpt, so excerpt, and we'll stick with that for now. So let's close all of our models so that we just have our controllers up and running. We can go ahead and also protect our blog controller, because we have that done, so there we go. And as far as this is concerned, we, of course, aren't going to be using Page. We're going to be using Post. So we will Post::paginate(5), and we will say posts paginate(5) admin.blog.index. Let's use model in this case, and let's also change this so that it's posts, just so that as we are looking at our blog controller, we know that we are working with posts. I can't tell you how many times from my personal experience when I just copy and paste and I think, yeah, I don't know the difference between the two. But whenever I have both files open and I'm looking at the code, I'm not looking at the filename or anything, it helps to change the variable name so that you know what you're working with. So because we changed the name inside of our controller for our model, we need to do that for blog. So let's go to index and we will change all of this. So that is blog.create. Everything's fine there. We do have more information here. We have not just the title, but we have the slug. I don't know if we want to put the excerpt, and we definitely don't want to put the body here. But we can also say, hey, we want the author. So let's have Title, Author, and then Slug. And we should also have an indication as to whether or not the post has been published. So let's just have Published there. Now let's focus on our foreach loop so that we make the necessary changes. We no longer have pages. It is now model, and let's also call each individual thing a post. And then for the link, we are no longer using the pages.edit for the route, instead, that is blog.edit, and that we need to supply information for that route. So we will have our blog there with the given post id. And then let's change the text so that it is going to use the post title. And then we just need the other pieces of information, such as the author of the slug and published. Although, let's leave the published blank for right now. So as far as the author is concerned, we, of course, want to get the user from the posts, but let's get the first. And then name, that will give us the name of the author. And then we want to display the slug, which is also going to be from our post. So we'll just use the slug property there. And then we will have an empty cell for the published date, and we will come back and add that there. Now, we do need to change our links. Instead of pages links, that is now model links. And that should be sufficient, at least to get started with. Now we need to set up our routes. So let's go ahead and do that. Let's go to web.php. Let's just take what we did with pages and use that. We'll change the pages to blog. We'll change the controller to BlogController, everything else will be fine. Now before we run this, let's go to our app layout so that we can add some links to easily get to our post list and the Create page. So basically what we did for our pages we want to do for our blog posts. So let's copy and paste what we have for pages, and then make the necessary changes. So for the text of this menu, it's going to be Blog Posts. The text for the list is gonna be Post List, and then Create New. And then we just need to change the routes that are used to create the URLs. So we are no longer using pages, we are using blog. And with that, we should have a usable menu. And let's give this a shot. So let's go to the browser. Let's go to admin. Okay, so we have, of course, we have pages. Let's go to the Post List, and we have our posts. So that looks great. Of course, our other views aren't going to work as well because everything is wired up for pages. But this is a good place to stop for this lesson. Because in the next lesson, we can start writing or editing the code for creating and editing posts.

Back to the top
View on GitHub