Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Laravel400
  • 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 TableSeeder, and our BlogController, 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 BlogController instead of a PostsController. And we need to remember to say Admin/BlogController. This will be a resource controller. And let's go ahead and set the model equal to 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 a database migration, so database migration, and it's not there, so let's refresh, there it is. And I'm just going to paste this in here. 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 time stamp. 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 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 one 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 a class, and that was PostsTableSeeder. So users are to find O. 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 from 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 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 BlogController, 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 will do the same thing. So public function, posts. And then we will return has many, 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 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 BlogController, 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 want 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 BlogController 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'll 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 file name or anything. It helps to change the variable name so that you know what you are 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 is 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 went to put the exert 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. And then we will say model as post. The route is going to go to blog.edit, and we will say post is post id, we have the post title, and then we want to display all of the other information. So the next is going to be the author, so we will output the author with post username. Then we will display the slug, so let's change page the post, url to slug. And then for right now, let's just have our published as blank, so we'll change this to model. And we need to add first here so after user, first and then name. Now we need to set up our routes so let's go head and do that. Let's look to web.php, let's just take what we did with pages and use that, would change pages to blog. We'll change the controller to blogController, everything else will be fine. Now, before we run this, let's go back to our views. And inside of our app layout, we're going to add a link for our BlogController. So somewhere around here, yes, here we go, after the manage users actually let's do before the Manage users. So we will add an li. Well, let's essentially do what we did with the pages, because we would want a link to create, as well as view the list. So let's just make the necessary changes, blog.index, blog.create, instead of page list, let's say post Post List, and instead of Pages here, we'll say Blog Posts, that will be fine. So let's php artisan serve, and let's give this a shot. So [LAUGH] 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