3.2 Displaying the List of Pages
All our content management capabilities revolve around being able to display a list of pages. We'll implement that list in this lesson.
1.Introduction1 lesson, 01:23
2.Getting Started4 lessons, 46:41
3.Managing Pages6 lessons, 1:12:31
4.User Management2 lessons, 27:37
5.Managing the Blog4 lessons, 41:51
6.Adding Extras2 lessons, 26:07
7.Implementing the Front-End3 lessons, 30:24
8.Homework Review1 lesson, 07:11
9.Conclusion1 lesson, 01:24
3.2 Displaying the List of Pages
Now we are ready to actually display some information in the browser. But first we need some information to display. So we're going to write a seeder for seeding our pages table. So php artisan make:seeder and we want to call this PagesTableSeeder. So let's hop on over to our code editor. We want to go to the database folder, and then seeds and then the PagesTableSeeder. So here's what we want to do. There is a relationship between the user and the pages that they create. So what we can do is, first of all, get a user, in this case we're just going to use the admin user to create our pages. So we will say user and we will find one with the ID of 1. So we are going to use App\User. We're also going to use App\Page as well to give us easy access there. So we will say admin, and then pages and we're going to save many because we want to create many of these pages, and we are simply going to new, Page and then we will pass in the necessary information. So what do we have? We have the title, this will be the About page, the URI, or the URL, I think we call it. Let's be sure let's go to the migrations, let's look at the create pages. We have title, URL and content, great, so that's going to be fine. And the URL is going to be /about. And then the content is going to be something very simple. This is about us, [LAUGH] for the lack of anything better. So let's just copy this. And we'll use this as a basis for a couple of other things. So we will have an about page, let's have a contact page. So we'll say Contact the URL will be /contact, and then You can contact us. And then one more page. What could this be called, let's just say, another page for the lack of anything else. And the URL will be another-page, and the content will be This is another page. Now we could just run db:seed again, and that would seed everything. But since we don't really want to mess up anything with our existing data, which really it's been seeded before, we're not going to be changing anything. We can run just our pages seeder, but first of all, we do need to truncate this. So we'll say Page::truncate() and there we go. Now let's go ahead and let's add this to our database seeder. Now because we use the users here, we need to run this after PagesTableSeeder. We're adding it here so that if we ever do run db:seed again, then it's going to be ran here. But for our purpose, we're not going to do that. Instead we're going to say db, or I'm sorry, php artisan db:seed, and then we're going to specify class equals, that was PagesTableSeeder. So we'll run that Everything looks good. I guess we ought to make sure that that is the case. So let's open up workbench and we will see if that is the case. Although while that's opening up, let's go ahead and close our seeder because we are going to write some code inside of our pages controller, although that has opened. So let's just take a gander here. And we have laracms, we have pages. Let's select there and we have our three pages. So everything's great, Yay!, we can close that we don't need anything else. So let's open up our pagesController. And what we want to do is retrieve all of our pages. So we'll say pages equals Page:all() that will get all of our pages. But then we want to pass these pages to our view because the view needs to know what data it's going to display. So we do that by passing another argument to our view method. It's going to be an array. The key is whatever we want to refer to our data in our view. So it's essentially the variable that we want to use. In this case, it makes sense to just call it pages, because that's what we are working with. And then we will have our pages as the value, so that's all that we need to do for this method. So now we just need to go to our index.blade.php file, and we need to display that information. So we'll start by taking out our little placeholder data there. And we will add a div with a class container. And then we will have a table with a class of table. I think that's the class that we need to use. And then inside of this table, we will of course, have our data, but let's first of all have our thead, where we will have our headers, and let's start with just the title and the URL. So we'll have a th Title. And let's just copy and paste that and that will be URL, eventually we want to display a little bit more information here, but this is going to be fine. But then we want to iterate over our pages and display a row for each page. So we're going to say @foreach so all of our directives are the same way. @if, @for in our case @foreach and, we have pages as page. And then we end that @endforeach. If we had an if it would be endif, if we had a for, it would be endfor and so on and so forth. So it's very straightforward there. And then we just have our markup. So for each page we'll have a row. The td is going to be the title, now in order to display the actual information we can't just say $page, and then title. We need to wrap that with two pairs of curly braces, that is going to signify to the blade engine that this is data that needs to be evaluated and then, put in that place. So we have the title there, then we will have the URL, and that's gonna be fine. So let's run this php artisan serve. And then I don't have a browser open, so let's fire one up. We want to of course, go to localhost at 8000. And then we look to be logged in, so we will go to admin/pages. And there we have our list of pages. Now let's go ahead and add a link for creating a new page because that's really the next logical step to do. So before our table, let's add an a element with an href, of well, what do we do? Now we could manually say admin/pages/create, but let's do this, Laravel gives us the ability to specify the name of a route and it will generate the URL for us. So here's what we do, we use our pair of curly braces, or at least our double pair of curly braces. We say route and then we specify the route's name. So here's the thing, whenever you create a resource controller, there are routes that are already defined and it's typically the name of the controller pages.create, in this case. So we'll actually see if that works [LAUGH]. We'll put the text as Create New, let's go ahead and give this some CSS here we'll say class btn. And let's see what that looks like in the browser. So let's refresh we have Create New that didn't look like I thought it would but okay, but notice down here at the bottom, the URL is admin/pages/create. So if we were to actually click on this link, it would work. We won't see anything because we haven't created a view for that route, but we at least have a starting point. So we are well on our way to writing our application we have some data to work with as far as our pages, we are displaying our list of pages. So now we just need to perform the rest of our CRUD operations. We need to be able to create a page, edit them and delete them. And we will get started with that in the next lesson.