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

Refactoring Popular Query0:00

Okay, welcome back. So in the last episode, if you're working along, I gave you a bit of homework. So we know that we can now add this popular query, and that will sort the results by popularity. And we can also do that with specific categories. So if I tack on popular, once again, we're now viewing only PHP submissions by their popularity. But right now, we don't have any way to sort them. So why don't we take care of that? And we're also going to tweak our query itself. So if I go to communityLinksQuery, if you remember, this is a dedicated class for performing one query to fetch the community links. Now, as we see here, we are fetching all community links, but we want to sort them by popularity, right? So we do that by joining the votes table. And then we grab the total count of votes for that specific link. And then we order by the voteCount if the user selects that. However, what I did not know is that recently, at the time of this recording,

Using withCount for Votes0:47

we grab the total count of votes for that specific link. And then we order by the vote count if the user selects that. However, what I did not know is that recently, at the time of this recording, a new method was added called withCount. So for example, if you ever want to say Post withCount comments, well, before, not even too long ago, at the time of this recording, you couldn't do that. But now you can. So what this will do is it's going to fetch you all the Post instances. But it's also going to give you a comment_count column as well, that contains the number of comments associated with this Post. This is hugely useful. So I'm glad it's been added. So that means we can rewrite this and simplify it a good bit, which is nice. For instance, originally, we were joining the votes just so we could fetch the count. But we don't need to do that anymore. So I can remove that entirely. And then say withCount and use the votes relationship. Next, yeah, we don't have to

Adding Sort Links UI1:32

just so we could fetch the count. But we don't need to do that anymore. So I can remove that entirely. And then say withCount and use the votes relationship. Next, yeah, we don't have to do any of this joining action anymore. We don't have to group it. And the only thing we do have to change is, originally, we set the column name to vote_count. But Laravel is going to use votes_count. So withCount votes, it'll create a new column and then add _count to that. All right, so yeah, quite a bit simpler. If we give this a refresh, I hope it's going to work. Popular. And yeah, we're still getting what we had before. So that's a nice little refactor. Excellent. So if we come back, that all looks good. Next, I'm going to go to community/index.blade.php. So here's the community tab right here. So what I want to do is, after the heading three, I'm going to add an unordered list where we have two links.

Preserving Category URLs2:16

index.blade.php. So here's the community tab right here. So what I want to do is, after the h3, I'm going to add an unordered list where we have two links. One will be for most recent. And then the second will be for most popular. Okay, so now, if I were to make this, for example, popular, and we go to the homepage, if I now click on this, it'll add on that query for us. Now, to go back to most recent, well, yeah, we could start by doing this, just have that take you back to the homepage. So yeah, that would work to start. But I also want to be able to use these links for the subcategories. So now if I add most popular, yes, I can sort by most popular php. But if I hit most recent, it's going to take me back to the homepage. So why don't we do this? Why don't we change this to request()->url(). So now, yeah, if

Styling Tabs with Bootstrap3:03

yes, I can sort by most popular php. But if I hit most recent, it's going to take me back to the homepage. So why don't we do this? Why don't we change this to request URL. So now, yeah, if we give this a refresh, go to the PHP section, I want most popular. If I click on most recent, it's just going to link me to this URL minus any kind of query. So that looks good. So let's see, most popular, most recent, or JavaScript, most popular, and everything's working. Okay, so now let's just add a couple things. Let's add a class here of nav and nav links. Is that it? It's a bootstrap. Nope, that's not it. Must be nav tabs. Yeah, this is just a bootstrap class you can add to create tabs. So now for example, if you were to add active, there you go. Okay, so now we need to dynamically add this class though, right? So what I could do is just say, well, actually, let's start with most popular. Let's do this. We're going to say, if the request

Dynamically Setting Active Tab4:01

Okay, so now we need to dynamically add this class though, right? So what I could do is just say, well, actually, let's start with mostPopular. Let's do this. We're going to say, if the request has a popular query within it, then we're going to add active. Okay, so come back, refresh, click on popular, and now we've dynamically applied that. For this one, well, what you could do is we could just duplicate that and then reverse it. So if it has popular, that's not the selected one. Okay, so now we can view all of our items, paginate through them. We can sort them by the most popular of the bunch. We can toggle our votes like so. We can view submissions only for one particular category and toggle those up or down. And then once again, we can sort by popularity specifically for that category. Okay, and that'll do it. Good job.

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