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

Add Filter Options UI0:00

Okay, let's work on these other filters here. So I'm only going to do a few here, but realistically, if this app goes to production, there should be more here. So let's go ahead and define them in our view first. So let's go to the ideas index, and the default one would be no filter. So let's do that one first. And there's no database here for the filters. So we can just hard code each one here. So let's do one for top voted. So this would be the most voted of all time.

So let's do one for top voted. So this would be the most voted of all time. We can do others too, like most voted recently, or most popular recently. But for this one, we'll just stick to most voted of all time. And the other one I want to do is my ideas here. So I'll say my ideas. And like I said, that's okay for now. But realistically, there should be more if this app goes into production. Let's also add a wire:model like we did for the category. So let's grab this.

Wire Filter to Query0:53

Let's also add a wire:model like we did for the Category. So let's grab this. And we can put one for filter. Okay. So now let's go into our ideas/index. And let's make a new one for filter. And let's make a new query string for filter as well. Okay. And that looks good. So we can do pretty much the same thing as our Category here.

And that looks good. So we can do pretty much the same thing as our Category here. So we need a new when clause. So let's grab this one. Let's paste that one in here. And this will be... Let's get rid of this use statement. We don't need this. And this one will be for filter. So let's replace category with filter.

And this one will be for filter. So let's replace category with filter. Okay. So the default is no filter. So let's change this. Actually, we need separate when clauses for each filter. So instead of not equals, let's say equals, and then take care of this case. So this case would be, say, top voted. And then we can work on the query here. So all we have to do is change this to orderBy descending votesCount.

Implement Top Voted2:58

So let's just say this one. Okay. And now let's set the filter to top voted, and the 21 is on top. So just like before, this should work with the other filters as well. So if I want top voted for all categories in category one, just select category one here. And you see the query string is updated as well for this new filter. Okay. So let's work on the next one. So the next one is my ideas.

Implement My Ideas3:20

So let's work on the next one. So the next one is my ideas. So just list every idea that I've posted, and you have to be logged in for this. So let's take care of that. So I am logged in now. So let's work on that one first. So again, we're just going to have to have another when clause right here. So when the filter is defined and the filter is my ideas, and this one is pretty straightforward. We just have to say query where the user_id is the person logged in, and we'll make sure the user is logged in in a second after I do this.

We just have to say query where the user_id is the person logged in, and we'll make sure the user is logged in in a second after I do this. So auth()->id(), and this should work. So let's try this. Let's go back to the default. So I'll just go back to the homepage, and I am signed in, and I should have no ideas here actually. I'm going to sign in as my other account, but let's just see. So, yeah, there's nothing here. So let me log out and log in as my other account.

So, yeah, there's nothing here. So let me logOut and logIn as my other account. Okay. And again, let's try that out. My ideas. And there we go. Here are all the ideas that I've posted. Cool. Okay. Let's signOut here and make sure to redirect to the login page if we're not signed in.

Redirect If Not Logged In4:33

Okay. Let's sign out here and make sure to redirect to the login page if we're not signed in, and we select that filter. So we can add a method here. Let's put it after this. Let's say updatedFilter. So anytime we update the filter or anytime the filter variable is changed, we can run this method. So first we can check if this filter. So if this filter is my ideas, then we can check if the User is logged in.

So first we can check if this filter. So if this filter is my ideas, then we can check if the user is logged in. So if they're not logged in, so if not, check, then go ahead and return redirect to the login route. Okay. So this should work. Hopefully, I actually think we don't need this program here. So let's try this out. So we're not logged in. Let's try my ideas.

So we're not logged in. Let's try my ideas. And we are redirected here to make sure we are logged in to use that filter. So now when we try to hit that, it should work. Cool. So, yeah, that's basically it. And as before, this should work with these other filters as well. So for example, the category, my ideas at our category three, there should be only two. So yeah, that's basically it. If you wanted to add more filters, you can just add a new clause here in our query.

Write Filter Tests6:07

So yeah, that's basically it. If you wanted to add more filters, you can just add a new clause here in our query. And that's pretty much all you have to do. So since this video isn't very long, let's go ahead and handle testing here as well. So I'm just going to grab the test that we did for the categories, and we'll just base it off of that. So duplicate, let's call it, what is this? OtherFiltersTest. Yeah, that's fine. OtherFiltersTest.

Yeah, that's fine. Other filters test. Okay. Let's name it otherFiltersTest. And what's the first test we want? Let's see if the topVotedFilter works. So let's start with that. topVotedFilter works. So let's make a few User here because we have to add some votes for them. So User B and User C categories are fine.

So let's make a few users here because we have to add some votes for them. So User B and User C categories are fine. Status is fine. Let's add a few ideas here. So we have ideaOne, and let's add ideaTwo. Actually ideaTwo is already here. Okay. And I'm also going to add some votes here. We don't need ideaThree here. So I'm going to paste it in to save some time.

We don't need Idea three here. So I'm going to paste it in to save some time. Let's import Vote. And we have one Vote for Idea one, a second Vote for Idea one, and one Vote for Idea two. So Idea one should be top voted. So we can do something similar here. Make sure to import Livewire. Actually it's already there. So we're setting the filter to top voted. Okay.

So we're setting the filter to top voted. Okay. And like I said, there should be two here because there are only two Ideas. So let's try that first. So I'm going to delete this. Let's put a semicolon here and let's try this one out first. Actually let me remove these other tests here. Okay. And let me run this test. See if it works.

And let me run this test. See if it works. And it does work. Let's add a few more conditions here. So let's say an Idea is first. So the first Idea. So this one. So the one at the top should have a votesCount of 2 because that's what we specified for our setup. So we can do this and it should be equal to 2.

for our setup. So we can do this and it should be equal to two. Let's try that should work. Let's run our test. Okay. And the next one should have a voteCount of one. So let me duplicate this. So that would be the next one, which there was a second method here, but there isn't. So we can do getOne and same thing. getOne votesCount should be one.

So we can do get one and same thing. Get one votesCount should be one. Let's run this and we are passing. So if I were to comment out one of these votes, this test should fail because the voteCount is incorrect. So let's run this. It should fail. Okay, cool. Okay. Let's work on the next test.

Okay. Let's work on the next test. And that's going to be testing the myIdeas filter. Okay, so let's say myIdeas filter works correctly when User logged in and will handle the other case when we're not logged in after this one. So we only need one User. Actually, no, let's have two Users here. So exchanges to be. Let's have three Ideas here. So we have two so far.

Let's have three Ideas here. So we have two so far. Let's make one more Idea three. Okay. And say the first two belong to User one, and then the third one belongs to User three. So let's go ahead and make our assertions. We don't need any votes of this as well. So we have to be logged in so we can use the actingAs method. So actingAs the first user, we can test this component that set the filter to my ideas. Okay.

So acting as the first User, we can test this component that set the filter to my ideas. Okay. And the count should be two as well, because the first two belong to User one. Actually, let me change these names here. So my first Idea, my second Idea, and my third Idea. Okay. So this one should be on top for this User because it's the most recent one. So we can assert against that exchanges. So the title should be my second Idea, and then the next one should be my first Idea. Okay.

So the title should be my second Idea, and then the next one should be my first Idea. Okay. Let's get rid of this one. And this should pass, hopefully. It does not. Oh, sorry. There should be getOne. So the second one. So let's remove this. getOne.

So let's remove this. Get one. Let's try that again. And it does pass now. So next test is the same as this one, but we just have to assert that it redirects back to the login page because we're not logged in. So let's duplicate this. My ideas filter works correctly when user is not. Okay. And we can leave this.

Okay. And we can leave this. We can remove the acting as because we are not logged in. And we can remove this. We can just assertRedirect to route login. And let's go ahead and try that out. And it does work. And let's do one more. And that one's going to be if it works with the other filters again, there's several cases for this, but I'm okay with just testing one case.

And that one's going to be if it works with the other filters again, there's several cases for this, but I'm okay with just testing one case. So let's go ahead and do that. So my ideas filter works correctly with categories filter. Okay. So two users, two categories, one status and three ideas. So first one is category one with status open. So this one is the same. For this one, let's change this one to category two. Actually let's change it to user as well.

For this one, let's change this one to Category two. Actually let's change it to User as well. So we actually don't need the second User. So let's put the acting as back acting as User. Okay. And we are testing. So let's set the filter to my ideas and also the category to Category one. Okay. So Category one that are my ideas and it's grabbed the assertive view has from up here as well.

So category one that are my ideas and it's grabbed the assertive view has from up here as well. So let me just grab this and paste it down here as well instead of this. Okay. So my ideas that are category one. So there should be two because this one's category two and they're all the same User. So the count should be two. The first one should be my second Idea. So this one and then the next one should be Idea one. So this should work hopefully.

So this one and then the next one should be ideal one. So this should work hopefully. And it does. Cool. Oh yeah. One more thing that I forgot I think was to reset the pagination if we select a different filter. So let's go back to our ideas index and we already have it for this for updating the category. We can do the same for updating filter.

Not this one. This one. This one here. So let's grab this one and let's name it something like no filters works correctly. So let's rename this. No filters works correctly. So we just need one User. That's fine. That's fine. We have three Ideas.

That's fine. We have three Ideas. We don't need this acting as. Let's test the ideas index and I set the filter to what was it no filter no filter and this should return all of the ideas. So three changes back to the original User. So this should be my third Idea because this is the latest one. And then the second one should be my second Idea. If I'm doing this correctly and let's go ahead and test that out. Okay.

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