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, 45:12
3.Managing Pages6 lessons, 1:12:31
4.User Management2 lessons, 27:40
5.Managing the Blog3 lessons, 42:25
6.Adding Extras2 lessons, 25:48
7.Implementing the Front-End3 lessons, 32:10
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 page's table. So php artisan make: seeder. And we're going 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 saveMany, 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 migration, 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 whole hack 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 DVC again and that would seed everything. But since we don't really want to mess up with anything with our existing data which really, it's been hidden 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 used the users here, we need to run this after PagesTableSeeder. We're adding it here so that if we ever do run DBC 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 pages. TableSeeder. 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 work bench and we will see if that is the case. Although while that is 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 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 pages controller, and what we want to do is retrieve all of our pages, so we'll say pages equals, and page all. That will get all of our pages, but then we went 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're 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'll 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 out 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 the we just have our markup. So foreach 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 up. We want to of course go to localhost, 8000, and then we looked 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 a element with an href of, well, what do we do? Now we could manually say admin/pages/create, but let's do this. Give us the ability to specify the name of the a route and it would 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 will actually see if this works [LAUGH] and we'll put a 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.