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

Creating spam report feature0:00

Okay, let's work on managing spam. So we'll start with marking an Idea as spam, which gives the users the ability to mark ideas as potential spam. So I'm going to keep this very simple. I'm just going to add a new spam_reports field on the database, default it to zero, and then every time a user hits that button, increment that count. I want to give it the same treatment as the deleteIdea model, and I won't make you watch me do that because I did that in the last video. So let's just make the Livewire component first, php artisan make:livewire mark idea as spam, and then I'm just going to copy and paste what I have from

Restricting spam menu to auth0:33

So let's just make the Livewire component first, php artisan make:livewire. Let's say mark Idea as spam, and then I'm just going to copy and paste what I have from the DeleteIdea model, and then I'll be back when that's done. Okay, so I have the model showing when I click mark as spam, and the text is changed appropriately. I want to make one change here. In here, in the idea.show, you'll see that I have this auth directive to check if the User is logged in. So I do want to make sure that the User is logged in to be able to mark the Idea as spam. But I'm going to wrap this auth directive, and I'm going to remove this one. I'm going to wrap it around the entire menu, or not the entire menu, but the entire listing.

But I'm going to wrap this auth directive, and I'm going to remove this one. I'm going to wrap it around the entire menu, or not the entire menu, but the entire listing right here, because I only want this to show if the User is logged in, and all the other menu items have their own appropriate authorization. So let's do that. So I am logged in, so it does show, but let's go ahead and put it around this div, I believe. No, it's this one, I think. So let's say auth, and it ends here. So I am logged in, so this should make no change. The menu still shows, but when I log out, this should disappear.

So I am logged in, so this should make no change. The menu still shows, but when I log out, this should disappear. And it does. Cool. So let me just log back in, and we'll work on that functionality. Okay, so I'm logged in. And when I click this, I have a wire:click set to a markAsSpam method. So let's go ahead and work on that. Oh yeah, and note that this is a lot of repeated code from the deleteIdea model, which we'll extract to a Blade component in a few videos.

Implementing markAsSpam logic2:20

Oh yeah, and note that this is a lot of repeated code from the delete Idea model, which we'll extract to a Blade component in a few videos. So let's go to the markIdeaAsSpam, and let's go ahead and make that method. So let's say markAsSpam, okay? And here's where the auth goes, again, very similar to deleteIdea. So let's just grab this, and let's paste that in. But we no longer need this, because we just have to make sure that the user is logged in. So if they're not logged in, we can abort. Let me just import this.

So if they're not logged in, we can abort. Let me just import this. But if they are logged in, then we can go ahead and increment that field on the database, which we haven't added yet. But let me write this code first. So this Idea will make a field on the ideas table called spam_reports. And we just want to increment that. So ++. And we want to save the Idea. So this idea->save().

And we want to save the Idea. So this Idea, save. And let's also emit an event. So this emit, say idea was marked as spam. Okay. So let's go back into our migration for ideas. So create ideas table. And let's add that field. So let's just make it an integer, and we'll put after the description here. So table integer, I named it spamReports.

So let's just make it an integer, and we'll put after the description here. So table integer, I named it spam_reports. And it's defaulted to 0. Default 0. Okay, let's php artisan migrate:fresh --seed. Okay, now we should have this new column in our ideas table called spam_reports, and it defaults to 0. Okay, now let's try it out. Let's go back here. Let's refresh.

This is just a simple explanation with no code.

So it's this one. So it should be the last Idea. So back to our database, sort by ID, and spam reports is one. So if we try that again, mark as spam, it should be two now refresh, and it is two. So it is working. Cool. So now as an admin of this site, we have this piece of data, which lets us know if this Idea could potentially be spam, and we could just delete it accordingly. So if you want, you can just make use of your database tool like I am in here and just delete it manually.

Adding admin spam filter5:14

So if you want, you can just make use of your database tool like I am in here and just delete it manually. Like I just said, you can make use of an admin tool like Laravel Nova or Invoker. But in this case, I'm going to add some functionality into our app that allows us to sort by the most spam reports and then delete them accordingly. So let's go ahead and do that. So let me just add some spam reports here. Let's manually add three for this. Let's say four for this and two and one. Okay.

Let's say four for this and two and one. Okay. So essentially, I want a filter that sorts by the spam reports. So I want this one first, and then this one, and then this one, and this one. And then I don't want to show anything else if there is no spam reports. So we do have this filter section that we added. So how about we just add a new filter in there? So let's go ahead and do that. So I believe that's in the ideas/index. So ideas/index.

So I believe that's in the ideas index. So ideas index. And let's see other filters. Yeah, right here. So let's add a new one called spamIdeas. So say spamIdeas. And I only want this available, or at least I only want it to show when we are an admin. So we can check if the logged in user is an admin. And we did that in the setStatus functionality. Is it in here?

Creating @admin Blade directive6:34

And we did that in the setStatus functionality. Is it in here? No, it's not in here. It's in the Idea show, I believe. So setStatus. So we can do this, but I'm going to be doing this a few times. So I think it's a good idea to make a Blade directive. So I want to be able to replace this with admin and endadmin. So let's quickly do that. So if you look at the Blade docs, you'll see the section for custom if statements.

So let's quickly do that. So if you look at the Blade docs, you'll see the section for custom if statements. And we can put this in the AppServiceProvider boot method. So let's grab this and make it for our admin functionality. So AppServiceProvider boot space to that in. Let's import Blade. And we want an admin directive. And all we have to do is first check if they're logged in. So if auth or return auth check and also check if they're admin. So and auth user is admin.

So if auth or return auth check and also check if they're admin. So and auth user is admin. Now we should be able to make use of this admin Blade directive. So let's try it here first. So we can grab this. We can get rid of these nested Blade directives, which is nice. Let's add it here. And let's add @endadmin here. So this should work. So this is the set status button.

So this should work. So this is the set status button. And I am logged in as admin. So it still should work. So let me hard refresh here. And we have an error. Sorry, there should be no value here because we don't need this param. Let's get rid of that. Let's try again. And the set status is still working.

Let's try again. And the set status is still working. Now we can make use of this in our filter here. So let's go ahead and do that. So back to our ideas index. Let's wrap this in our new directive. And admin. And now it should only show when we are an admin. So I am logged in as an admin. Let me just hard refresh.

So I am logged in as an admin. Let me just hard refresh. And we see that new filter. So just to make sure, let me log in as a non-admin to make sure it doesn't show. So let me just grab another User here. And log in. Okay. And now you see that it doesn't show here. And also setStatus does not show here. Okay.

And also setStatus does not show here. Okay. So let me just log back in as the admin. Okay. So we want to add some new functionality for spamIdeas and sort by the most reported spam. So let's do that. So let's go to our ideas index. And let's grab one of the when clauses here. So we'll grab this one here. And we'll put it underneath my ideas.

So we'll grab this one here. And we'll put it underneath my ideas. So let me just paste that new when clause in. I guess we don't need this. So let's change this to whatever I named it, which is spamIdeas. And let's add that new clause on the query. So let's say query where the spamReports are greater than zero. So spamReports greater than zero. Okay. And it's also orderBy spamReports.

Okay. And it's also orderBy spam reports. So orderBy descending spam reports. Okay. Let's see if this works. And let me just fix this here. Okay. Let's try this out. So I do have ideas here. I think I just added them.

So I do have ideas here. I think I just added them. Yeah. 4, 3, 2, 1. So there should be four in this case. So again, let me hard refresh spamIdeas. And we do have four, 1, 2, 3, 4. So let me just make sure. So first one should be erom. And it is cool.

So first one should be erom. And it is cool. So it would be nice if I could see the actual spam report count right within here. So let's go ahead and do that. So let's go to our ideas index or idea index, I believe. Idea index. And it's added somewhere here. So right before the description, I believe it's here somewhere. I should see. So let me just make a <p> tag here, or let's make it a <div>.

I should see. So let me just make a <p> tag here, or let's make it a <div>. Say textRead, put a margin on it. And let's just say spamReports, whatever, four. And let's just see if that renders in the right spot. So we only want this to show when we're an admin and when there's at least one spamReport. So let's add those conditions. So let's say admin. And let's also add the condition with at least one spamReport. So let's say if idea->spamReports greater than zero, then go ahead and render this.

And let's also add the condition with at least one spamReport. So let's say if idea->spamReports greater than zero, then go ahead and render this. Okay. And let's see if this looks okay. Oh, yeah, we have to change this. So that's idea->spamReports. Okay. And it should be four, three, two, one, four, three, two, one. Okay. And let's add it for the show page as well.

Okay. And let's add it for the show page as well. So I'm going to grab all of this, go to idea.show, look for description here and add the same thing. Okay. Let's see if it shows here and it does. So now as an admin, I can easily check out every Idea that has at least one spam report and I can act accordingly. So just go to this filterSpamIdeas. And like I said, this only shows for admins, but I didn't add any security to check if

Adding mark as not spam13:50

and I want to reset the spam report counter. So I want another menu item here that says maybe not spam. And all that does is reset the spam report counter, like I just said. So again, same process as delete idea and mark as spam. So let me just create that Livewire component again. Let's call this one MarkIdeaAsNotSpam. Just like before, I'm just going to copy and paste what I have from the other component and I'll be back when it's done. So I have the model showing for MarkIdeaAsNotSpam shows up as not spam here. There's the model and I have it set to only show if there's at least one spam report.

So I have the model showing for markIdeaAsNotSpam shows up as not spam here. There's the model and I have it set to only show if there's at least one spam report. So if I go to one that doesn't have a spam report, then it will not show. So let's go ahead and work on this functionality. Pretty straightforward. Again, pretty much the same thing as markAsSpam, except we're resetting the counter. So the wireClick is set to markAsNotSpam. So let's go to that component. markIdeaAsNotSpam. Actually I have to add the mount method.

Mark idea as not spam. Actually I have to add the mount method. So let me just do that first. Actually just make that method first. Mark idea as not spam. And let me paste in the mount method. And again, I don't think you actually need this for the latest version of Livewire. I think if you just do this, then we don't need to initialize the idea. But since I've been doing it this way, I'm just going to keep it consistent. So let's go ahead and add the authorization here.

But since I've been doing it this way, I'm just going to keep it consistent. So let's go ahead and add the authorization here. So let's go back to our markIdeaAsSpam. Let's grab this. So we want it to be only admins can do this. So let's add the admin condition or $user is admin. Actually is not admin because we're doing the case where it fails. Let's import Response. And all we're doing is resetting the count. So let me just grab this.

Okay, the model did close, but didn't update here. But if I refresh, it did update. So that's because we just have to refresh the Idea on the IdeaShow component like we did before. So we didn't need it for delete because delete redirected back to the index page. But we do need it for these two. markAsSpam and markAsNotSpam. So let me just add those two. Okay. And let's add those two cases here.

Okay. And let's add those two cases here. Okay, so I added those two methods, and now it should update when I mark as spam. Okay, and it does. And the same should happen when I mark it as not spam. And it does work. Cool. So again, as an admin of the application, we have the ability to mark the Idea as spam or actually anyone can do that. But we can go in and see if any of the ideas are marked as spam, and we can delete them.

Okay, log in. And yeah, it doesn't show, but we can still mark it as spam if we want to. And again, we'll make sure to add an alert in the next chapter. So as always, let's go ahead and make a commit here. This is episode 43. git add, git commit, episode 43, manage spam.

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