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

Extract List Partial0:00

Okay, let's get back into it. Now, one thing we haven't yet done is, well, we are paginating the results, but we're not displaying any links yet. Okay, so back to phpStorm. I'm in the community/index page. So this is basically the view for this area right here. So why don't we do this? To start, let's clean things up. Why don't we take the unordered list, and we're going to extract that into its own partial. Maybe include community.list. Okay, let me create that. list.blade.php. And just to confirm, if I give this a refresh, it's gone. If I paste it in, it's back. Okay, sometimes I do that just for a sanity check. Anyways, I think that looks fairly good. Okay, so what else? Instantly, you can see we've cleaned things up drastically, and it's a lot easier to take in this page. But now, we still haven't added the pagination links. So maybe we could do that, I don't know,

Render Pagination Links0:51

you can see we've cleaned things up drastically, and it's a lot easier to take in this page. But now, we still haven't added the pagination links. So maybe we could do that, I don't know, maybe right here. Now, don't forget, we've already paginated the results using the paginate method here. So paginate into a set of 25. Now, just because we don't have that many, let's make it three, only temporarily. Okay, anyways, we could say links, and just coincidentally, in this case, the method to render the pagination links is also called links. But in any other case, like posts or articles, it's going to be something different. Just a coincidence. Okay, so if we come back and give this a refresh, we now have our pagination. So if we click to, yeah, you can see it just works right out of the box, which is really great. All right, so that takes care of that. Just a little bit of cleanup here. The next thing I'd

Make Channels Clickable1:37

yeah, you can see it just works right out of the box, which is really great. All right, so that takes care of that. Just a little bit of cleanup here. The next thing I'd like to do is make the categories or the channels clickable. So like right now, we only have PHP, but imagine we add another one. So I will do this manually. Let's just see the layout here. Okay, title, slug, and color. So let's create a new one. title will be JavaScript. The slug will be JavaScript, and the color will be green. Okay, so now if I come back and give this a refresh, we have two channels in the database. Let's quickly add one, some JS article. And once again, this will go to a dummy URL. Okay, so if we contribute this link, there it is at the top. And now when I click on any of these channels, well, for one, it needs to be clickable. So it needs to be a hyperlink, but it should then filter the results down to only those in that particular

top. And now when I click on any of these channels, well, for one, it needs to be clickable. So it needs to be a hyperlink, but it should then filter the results down to only those in that particular category or channel. Okay, back to phpStorm. We're going to go into our list here. And yeah, right here, this label should now be clickable. Maybe we start by making this an anchor tag, and then we'll set the href. We don't know yet. So give that a refresh. Still the same thing, but now at the very least, it is clickable. Okay, so the href should be, let's think about this. Maybe it goes to /community, and then the slug. So for example, if you go to /community/PHP, that's only going to show you a paginated list of PHP specific submissions, or JavaScript, you get the idea. Why don't we do that? Okay, well, to make this dynamic, we could just say link channel slug. And why don't we see what we get here. So we're going to give

Add Channel Route3:15

or JavaScript, you get the idea. Why don't we do that? Okay, well, to make this dynamic, we could just say link channel slug. And why don't we see what we get here. So we're going to give that a refresh. And if I hover over this, and you look at the bottom here, yeah, community / JavaScript, and then community / PHP. Exactly what we want. So if I click on that now, we don't have an endpoint set up for that. So we'll take care of this, add another one, route get community / channel. Why don't we, we have two options. One, this can go to a different method. Or maybe we can factor that in since most of the logic will be the same. So let's do this. Let's play around. CommunityLinksController will now accept a optional channel. And we'll update the doc blocks. Okay, so let me show you how this works. We're going to use route model binding, but I don't think it'll work just yet. So let's try this. If we go to /community,

Bind Channel by Slug4:09

Okay, so let me show you how this works. We're going to use route model binding, but I don't think it'll work just yet. So let's try this. If we go to /community, we do get a Channel instance, but notice that it's just an empty model. It hasn't been populated with any attributes, as you can see here. Okay, let's go to community/PHP. And we get the same thing. And that's because if we're going to use route model binding, it's trying to find the Channel with an ID of PHP by default. And it's not going to work, right? But if we use something like 1, well, now you can see it did find that record or 2 for JavaScript. Okay, so how do we change it, we could either not use route model binding. Or it would be cool if we could say, when you track down this record, what I really want you to do is search according to the slug. Well, here's how we do that Channel. And let's add it right here, getRouteKeyName.

when you track down this record, what I really want you to do is search according to the slug. Well, here's how we do that channel. And let's add it right here, getRouteKeyName. Okay, so once again, this is only applicable if you're doing the implicit model binding. So it will try to find by default the record by its ID. But in our case, we wanted to use the slug. And really, you don't have to do this. It's just if you want the convenience of being able to, for example, accept a wildcard, and then have Laravel behind the scenes immediately track down that record for you. So now I type in channel, and I will instantly get the record. This is opposed to doing something like channel, where the slug is equal to what we pass in and give me the first, you know, you get the idea, right? It's just a bit of sugar to do it for you. Okay, so I backtracked here. If we go back to Chrome, now if I give it a refresh, we should be able to track it.

Filter Links by Channel5:41

first, you know, you get the idea, right? It's just a bit of sugar to do it for you. Okay, so I backtracked here. If we go back to Chrome, now if I give it a refresh, we should be able to track it down. And we do. So that means how can we just say, give me all CommunityLinks where the channel_id is equal to this channel, but only if one was passed in. Now we can do this a number of ways, of course. You could say, if we have a channel, then I want to do this query. Maybe that might be fine. We could also say CommunityLink, let's add a scope here for the given channel. And let's try this out. Okay, so down, where should we put this? How about right here? Scope for channel. That will accept the queryBuilder, as well as the channel that we pass in. And then we're just going to say, well, if you gave us a channel, then we will say return builder, where the channel_id is equal to the id of the channel. Otherwise, there's no channel,

And then we're just going to say, well, if you gave us a channel, then we will say return builder, where the channel ID is equal to the ID of the channel. Otherwise, there's no channel, so we will just return the builder directly there. Yeah, I think that might do the trick in this case. So I'm sorry, I got rid of that. Let's bring that back. But now, do you notice that this query keeps getting a little bit longer and longer? This is what I've been talking about. And especially once we start factoring in voting for each lesson, we're probably going to have to use some joins. And at that point, we will extract this out of the controller method. But until then, let's just keep it here. So why don't we do this? Let's dd the links. If I come back and refresh this, let's see what we got here. Okay, so we have one link with the channel ID of one. And we know that channel ID of one is php. So there's two. Notice it excluded

If I come back and refresh this, let's see what we got here. Okay, so we have one link with the channelId of one. And we know that channelId of one is PHP. So there's two. Notice it excluded the JavaScript category. If I change this to JavaScript, we should just get the one and we do. Next, if I do /community, we don't get anything. You know what, I know exactly what this is. If we go back to our query scope, channel will always be true. Of course, I just got done explaining to you that channel will never be null. Worst case, it would be an empty channel instance. Let's say here, channel exists or even if we have the ID. So you'd only have an assigned ID if it exists in the database. Anyways, now that should give us all of the links and it does, including JavaScript and the PHP ones. Okay, so that means if we come back, that's at least our first pass. We can see how that feels to use. If I click on this, now I see only JavaScript.

including JavaScript and the PHP ones. Okay, so that means if we come back, that's at least our first pass. We can see how that feels to use. If I click on this, now I see only JavaScript. Or if I go to PHP, now I see only the PHP ones. But now have you noticed, there's no way to switch back to JavaScript or anything else. So we need to tweak this. Let's go into the index view. And to start, this will now be a hyperlink that just goes to the default view. Okay, so give that a refresh. Let's go to the main page. We can go to JavaScript or back, and then PHP lessons or go back. Okay, next, why don't we do this? We're going to pass through the channel as well. That way, within our index view, we could say something like, we could say something like, how about right here, if the channel exists, you know what, we did ID earlier. Yeah, you can do that. But let's just do exists. That's more readable in

we could say something like, how about right here, if the channel exists, you know what, we did id earlier. Yeah, you can do that. But let's just do exists. That's more readable in my mind. Anyways, if it exists, then we'll have a span where we have a dash, do in dash, and then we'll echo out the channel's title. And I think that will do the trick. Let's see. So back to Chrome. Yeah, so our main page has nothing. But we can go to JavaScript, we're back, and then PHP, we're back. So why don't we go back to the community link real quick. Yeah, we can change that to exists. I like that better than add my doc blocks, scope the query to records from a particular channel. Okay, that will be our Eloquent builder. And then this will be a channel. Okay, so you know what, I think we're going to call it a day for this particular lesson, making a lot more progress, we added actual pagination,

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