تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Creating Routes and Controller0:00

Welcome back. So, if we get back into it, I think we need to create some routes for our community links. So, I will say route.getCommunity, and we're going to point that to a CommunityLinksController, and maybe index on that. Okay, so this route will be responsible for displaying all community links. Next, I'm going to duplicate this. If we post to that endpoint, well, that's going to hit a store method, and that's what we're going to use to create a new community link. Okay, php artisan make:controller CommunityLinksController. Okay, so if we switch over to that now, whoops, the controller version, we now know that we have an index method that is responsible for displaying all community links, or at least a paginated list. So, I'm thinking we're going to, to start, we're going to

Building Index View0:44

have an index method that is responsible for displaying all community links, or at least a paginated list. So, I'm thinking we're going to, to start, we're going to return a view, and I'll call this community.index, maybe something like that, and we'll probably have to send some stuff through. So, let's say down here we're going to create a new file within resources/views/community/index.blade.php. Okay, so if I were to start, you know, we could say community right here, and if we go back to Chrome and we visit community, there we go. Okay, so let's get back into it. If we go back to resources, into our views, because we ran that php artisan make:auth command in the previous lesson, well, we have a layout file that we can extend and modify however we need. Alright, so let's do that. Let's say

Fetching Links With Pagination1:29

that makeAuth command in the previous lesson, well, we have a layout file that we can extend and modify however we need. Alright, so let's do that. Let's say extendsLayouts.app, and then for our main content section, yeah, here is where we can nest our h1 or whatever. So, now we should get a master page out of the deal, and we do. Why don't we go ahead and wrap this within a container or something like that to center it on the page. Okay, so you get the idea. Next, we want to display all community links. So, how do we do that? Well, we begin by fetching them from the database, and maybe, I know for a fact actually this is going to get more complicated, but for now, as the very first step, we could just say communityLink. I will import that at the top, and we're going to paginate them.

Creating Factory Seed Data2:13

going to get more complicated, but for now, as the very first step, we could just say community link. I will import that at the top, and we're going to paginate them into sets of 25. Alright, so we'll save that to links and then pass that through ultimately. But right now, of course, we don't have any community links at all. So, let's do this. Let's go to our factories file and quickly whip up one. We're just going to do a couple for now, but then later this is going to come in handy. So, community link, and the makeup of that, if we just go back to create community links table, yeah, we need the user ID, the channel, the title, the link, and whether it's approved. The user ID, we will whip up a fake User for that. So, App\User::create(), and then get their ID. Next, the channel ID, yeah, let's do, well,

whether it's approved. The user ID, we will whip up a fake User for that. So, User::create, and then get their ID. Next, the channel ID, yeah, let's do, well, do we want to do that? Why don't we just default to one for now, and then later maybe we can make it more dynamic. Anyways, title will be a fake sentence, and then the link will be a fake URL. And you know what? Let's just be explicit. We'll say approved is false. Okay, so if you're not familiar with database factories, it gives you one place to define the blueprint for a particular model. And in this case, we're using fake data, so that every time you call this function, you're going to get different data in response. So, it's a nice way to accomplish database seeding. Okay, so, for example, if we boot up php artisan tinker, and I say

function, you're going to get different data in response. So, it's a nice way to accomplish database seeding. Okay, so, for example, if we boot up php artisan tinker, and I say I want a factory for a CommunityLink, and we create that, let's see what we get. There we go. So, we have a new CommunityLink that's been saved to the database. And if you want to see that, let's just fetch everything in that table, and there it is. So, why don't we, let's tell you what, let's do maybe three more. And notice each of those, by the way, will be associated with different Users. Okay, so now, if we go back, we have some actual links to fetch. So, we grab those, and then we pass them through to our community/index view. And then here is where we could say forEach links as

Rendering Links in Blade4:16

links to fetch. So, we grab those, and then we pass them through to our community index view. And then here is where we could say forEach links as link, maybe we could put this within a list item. Maybe we'll do this, a class of community-link. And then up here, maybe our wrapper, we'll give it a class of community. I don't like them to be so similar. So, let's do this, links. Maybe if we want to follow the BIM convention, we could do that. Or, you know what, this is a toy project, let's just call it links. Okay, anyways, now we could echo out the link and the title. Let's come back to Chrome, give it a refresh, and we should now see about four. Okay, however, we should wrap it, right? And the URL will be link, link, actually in this case. Come

Adding User Relationship and Timestamps5:45

general, it tends to annoy people. Next, why don't we add one more section, like a small down here, and we'll say contributed by. Let's see where that is. Yeah, contributed by link. Well, yeah, we can get the userId, of course, but what we really want, in this case, is the userName, something like that. So, it would be nice if I could say, let's see, link. We could say the user associated with the link, or maybe the creator of the link. Maybe that could point to the User, and then we could get their name. Let's try that. So, it's gonna fail, right? And that's because our CommunityLink doesn't have that relationship. So, creator, what is the relationship? A CommunityLink belongs to a User. So, we'll say this belongs to a User. And now, finally, we're not using creatorId, we're

creator, what is the relationship? A CommunityLink belongs to a User. So, we'll say this belongs to a User. And now, finally, we're not using creator_id, we're using user_id as the foreign key. Okay, so if we go back to Chrome and give that a refresh, there we go. Contributed by this person. Next, before we finish up, how do we specify, if we go back here, how, I'm sorry, if we go back to our view, how long has it been since they contributed it? So, I really want to say contributed by Jeffrey Wei ten minutes ago. We could do that by saying link->created_at, or actually let's change this to updated_at, and I'll show you why in a couple lessons. But for now, they're gonna be identical. So, if we refresh, not what we want. We want this in human readable form. And luckily, because these columns,

couple lessons. But for now, they're gonna be identical. So, if we refresh, not what we want. We want this in human readable form. And luckily, because these columns, these timestamps, by default, are instances of Carbon, which is a fantastic library, we can do stuff like this, diffForHumans(). Now, if we refresh, oh my gosh, it's so easy, right? Figuring even something like that out years ago on your vanilla PHP project, it just sucked. You always had to research it for an hour. But now, it's a non-issue. Now, what else? We might, like in real life, your application, maybe your users have profiles, or maybe they can link to their own website or something. So, in that case, maybe this would be an anchor tag. But, yeah, I don't have anything, so I'm gonna leave it off. But you get the idea.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟