Add NotifyAllVoters state0:00
Okay, let's work on this NotifyAllVoters feature, which should send out an email to everyone who voted for this Idea whenever the status is changed by the admin. So let's work on that. Let's add a new piece of state to check if that box is checked. So NotifyAllVoters, and let's wire:model that up here in our checkbox. So wire:model, NotifyAllVoters, okay. And I'm going to remove this checked too. Okay. Now in the setStatus, we can check for that after we update the status. So say if this NotifyAllVoters, then go ahead and do that.
Create helper notification method0:37
Now in the set status, we can check for that after we update the status. So say if this NotifyAllVoters, then go ahead and do that. So I'm going to put it in a helper method for now. So this NotifyAllVoters as a method, and then later on, we'll move it to a job. So let's grab this. Let's make a new method here. NotifyAllVoters. Okay. And we need all the users that voted for this idea, and we already have that relationship on the idea.
And we need all the Users that voted for this Idea, and we already have that relationship on the Idea. So let's go ahead and grab it, and let's try and dump it to see if we are getting the correct thing. Okay. So let's see if I did this correctly back here, refresh, change the status, NotifyAllVoters, and let's go ahead and update. Okay. So we do get two Users, and these two Users voted for this Idea. So let's go ahead and loop over these Users and send them an email.
So we do get two Users, and these two Users voted for this Idea. So let's go ahead and loop over these Users and send them an email. So first, I'm going to just grab the name and the email. So let me change this to a method. Let's select. Actually, let's put this one online. Let's select just a name and email, and let's go ahead and change it to a collection. So use get here, and let's assign this to a variable called voters. Okay. And now we can loop over all of these voters.
Okay. And now we can loop over all of these voters. So for each, say, voters, say, as user, then send out an email here. Let's just dump the user to see if we did that correctly as well. Okay. Back here, refresh, change the status, update. I forgot to check the box. Change the status, update. I mean, check the box and then update. There we go.
I mean, check the box and then update. There we go. So we do get the two users, and we're only getting their name and their email back, which is all we need to send the email. So this is fine. But if we have a large number of votes, then this may cause issues if we're loading too many users into memory at the same time. So say, for example, we had an Idea with thousands of votes, and at this scale, it probably won't be an issue. But say we did.
Chunk voters for memory3:00
be an issue. But say we did. Let's say millions. So we'd have to load millions of User instances into memory at once, and you might hit the memory limit. So how can we change that? So the easiest solution would be to chunk the data and process it in different chunks. So let's do that. So let's say chunk and just give it a number, and I'll just do 100. So we'll process it 100 items at a time.
So let's say chunk and just give it a number, and I'll just do 100. So we'll process it 100 items at a time. And then the second argument is a callback, voters. And in here, we can just put this in there. So let me move that. Okay. And we don't need the get anymore. So this should work exactly the same way. But if we have more than 100 users, then it should process them 100 at a time in different chunks.
But if we have more than 100 Users, then it should process them 100 at a time in different chunks. And we no longer need this variable as well. So let's get rid of this. And let's try this out. Again, it should be the same thing because we only have two Users here. Change the status, notify all voters. And we do get the same thing. Okay. So here is where we want to send the email out.
Build and queue mailable4:08
Okay. So here is where we want to send the email out. So I'll send email. So let's go ahead and make a mailable. So say php artisan make:mail. Let's call it IdeaStatusUpdatedMailable. And I want it to be markdown mail. So we'll do markdown equals, let's say emails.idea_status_updated. Okay. Sorry.
Okay. Sorry. I meant to put a dash here instead of a dot. So let me just delete what I did and redo it because it already exists. So I just deleted them. Let's do it again with the correct dash. Okay. So let's go ahead and send the email here. So we can use the Mail facade here, Mail::to, we're sending it to the $user. And let's use the queue method.
So we can use the Mail facade here, mail to, we're sending it to the User. And let's use the queue method. Normally you want to make use of queues when you're sending out email and the mailable we're using is the IdeaStatusUpdated Mailable. Okay. And we're going to pass in the idea here. Okay. Let's also make sure to import Mail. And right now the mail driver is set to the default mail mailer. So let's set it to log for now.
And right now the mail driver is set to the default mail mailer. So let's set it to log for now. Okay. And our queue driver is set to default, which is sync. And again, we'll just leave this for now. And then later on, we'll move it to Redis using Horizon. Okay. So let's go into our Mailable. So IdeaStatusUpdated Mailable. And let's go ahead and accept the idea here.
So Idea status updated mailable. And let's go ahead and accept the Idea here. So let me just use this, say idea. And let me just type in this with the idea and import it. Okay. And that should be good. Actually, let's make this public. Okay. And now let's work on the markdown email. So Idea status updated.
And now let's work on the markdown email. So Idea status updated. Okay. Let's change this title or header to Idea status updated. And let's say something simple here, the idea. And we have the idea here, so we can say idea title, say has been updated to a status of. And then we can do the status here. So again, we can grab that from the idea, ideaStatusName. And this is a button. Let's take them to the actual Idea page.
And this is a button. Let's take them to the actual Idea page. So I'll say viewIdea. And then we can just use the route helper and say idea.show and then pass in the idea. Okay. This is fine. And that looks pretty good. I think we also have to set our mail. Where is it? Our mail from address here.
Where is it? Our mail from address here. So let's just put something in here. Okay. And hopefully I did everything correctly. Let's go ahead and try it out in the browser. Refresh this. Let's change it to implemented. Let's check this box. Let's update.
Verify emails in logs7:39
Let's check this box. Let's update. Okay. So that did look like it worked. Let's check the logs to check if the email did get sent out. So laravel.log. I think you have to open it once in VS Code for it to show up in Command P. So let's go ahead and do that. So it's in the storage/app/logs, laravel.log should have cleared it first. But if we go all the way down, let's see if there's emails that are sent out and looks.
So it's in the storage/app/logs, laravel.log should have cleared it first. But if we go all the way down, let's see if there's emails that are sent out and looks like there is. So here's one from admin at admin.com to this person. And I believe there's one up here too that goes to the other person. And if you remember, there's only two voters here. So there should only be two emails up here. Yeah. It looks like there is. So yeah, here's the other one.
Use local mail client8:53
with emails locally in development. So let's go to settings and let's add this in. And then it should catch emails in this client instead of the log. So let's go back to our .env file. Let's paste that in. And let's comment these out. So up to encryption. Okay. And this should now work in this mail client. So one more time.
And it looks like I forgot to set the subject. So we'll do that in a second. But the email does render out correctly and is sent out to the correct people. So if you view Idea, it does open that Idea. Okay. So you can change the mail from name as well. So you can see it's from Laravel, which is the app name. You can change that in the .env file here. So mail from name. So we can set the subject in the Mailable.
Add email subject9:59
So mail from name. So we can set the subject in the mailable. So let's go to that. And we can do it here. So before we call the markdown method, let's call the subject method and set a subject here. Idea you voted for has a new status. Okay. Let's save that. And let's try it again.
Test Livewire loading state10:23
Let's save that. And let's try it again. Actually, let me try a different Idea, which has more votes. So let's try this one has 21. Now this should take a while because our Q driver is set to sync. So I also want to check if there is a disabled style here while it's loading. So let's actually work on that before we send the mail out. So that should be in setStatus. So let's go to that. So it's this button here.
Let's see if it shows this style when it's being processed and Livewire does disable the button when it makes a request. So it does that for us automatically. So let's remove the disabled state and let's go ahead and try this out. Okay. Let me refresh, change the progress here or the status to implemented. Let's go ahead and notify all voters. And this should take some time. And there you see the disabled state while it's processing and you can see all the mail coming in.
Plan job and commit11:38
And there you see the disabled state while it's processing and you can see all the mail coming in. So there should be 21 emails here and it looks like there is the subject has changed and everything seems to be working. Cool. So yeah, in the next video, we'll move some logic to an actual Job like you saw earlier. Right now it's just on a method here on the setStatus Livewire component. So we'll move this to a Job. We'll also use Horizon for queues and then we'll test everything. So as always, let's make a commit here.
We'll also use horizon for queues and then we'll test everything. So as always, let's make a commit here. This is episode 34, git add, git commit, episode 34, say set status, notify all voters, sorry, 34.
