Laravel: 3 Ways to Send a Welcome Email (Controller vs Observer vs Events)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this video i want to show you three ways how to send a welcome email after new user is registered in your laravel application like this one on the screen so if you want to welcome someone with a simple email i will show you three ways how to do that and in fact this video will be applicable to anything that you want to do after user is registered send the email assign something create something something like that let's see the code so if you are using something like laravel ui or if you are on laravel 6 and 7 this is the default register form or it may be with a different design if you changed it but in reality you should have register controller so app http controllers auth register controller and at the bottom there is a function called create which is actually responsible for creating users so i have created an email notification already with php artisan make notification like this one and that welcome email notification is here and i've changed a few words to mail welcome to our project and this but it's a typical simple email notification and how to call that from register controller so first way to send the welcome email is directly from the place where you register the user with laravel ui its register controller's create method which needs to return the user so let's assign that user to a variable we still return it so return user but in between we can do whatever with that user so in our case user notify new welcome email notification and that's it and a new user registration let's actually test it fake filler chrome extension register a few seconds to send the email and in my mail trap i should see a new welcome email notification so that works you can send the email or do whatever with that user assign something immediately after you create it in register controller just don't forget to return that user and if you are using laravel 8 or laravel jet stream it's a pretty similar thing just in a different file i've opened it on github it should be app actions fortify create new user if you scroll down that file you see almost the same thing so it creates the user and returns it so you need to also assign it to a variable and you can send a notification here and generally first way to notify is wherever you create the user you send a notification second way is with observers so i have opened the documentation of laravel observer is a class that would observe on any changes of the model so created record updated record or deleted record and in our case we need to observe the created user so if you want to send a notification email from all the places where user could be created whether it's admin created a user teammate created a user or they register themselves so observer will fire no matter where that comes from and let's create that observer let's copy and paste and this is exactly what we need model user so we attach observer to the model paste here created successfully user observer should have been created so methods created updated deleted and restored and all of that and we care only about created and we have a user here so what do we do here user notify new welcome email notification same thing so let's revert the change here like this we don't need those anymore and we will test it so we have observer which has created which sends the notification the only thing left we need to register that observer in app service provider in boot method like this so app service provider in boot we register model so user to user observer class php storm will auto-complete the use here on the top and let's try it out let's register again log out register fake filler chrome extension register and do we have an email oh i know i've made the silly mistake that i told you not to make in register controller see what i've forgotten return so i need to return that user since that function doesn't return anything it throws an error on login because there's nothing to log in no given right so let's try again register with another user and that should work yep i'm logged in and in fact there should be two emails so that email was sent successfully because the user was created but not logged in successfully so email notification and successful email notification i intentionally don't cut the errors and the mistakes and the bugs because that may help if you encounter the same bug and also that shows that i'm a human being too that i make mistakes quite often but i'm pretty quick to fix them and to identify where they come from but that comes from experience actually you've seen that error before and you know exactly where to find it so as i often say if you want to become a better developer just code a lot you will encounter a lot of situations and you will be quicker to fix them okay back to our own situation enough philosophy so we covered the second way observers but that may not be exactly what you want so for example you want to send that email on registration and whenever teammate create the user but not when admin creates the user so then observer doesn't fit because it fires every time and you could potentially write a notification in here in register controller or in some user controller where the user is created but maybe more elegant way that comes directly from laravel default is events and listeners so in the register controller of laravel ui or laravel 7 you would see that it is powered by a trait called registers users and inside that trait there is a logic of all the registration or most of it and this is the public function register which calls the function create from the request and then see what happens event new registered so inside of laravel auth function there is event called registered and laravel itself fires that event with a parameter of user and then there should be a listener class that listens for that event and does something and this is also already implemented if you go to event servers provider in laravel app providers event service provider there is an array of what classes should listen for which classes so this array contains events listener classes and by default in laravel this is the code not written by me by default there is already event registered class listened by send email verification notification if we click that and go here it's a listener that inside of it fires the email verification notification if the user is with enabled verification of email so that function is from laravel internals i think it was introduced in level 6.2 or something but in the event service provider you may add more listeners so the same event is fired all you need to do is to create that listener so you do php artisan make listener welcome email listener for example and we just added here welcome email listener class and inside of that listener we have handle event and somehow we need to have a user here let's take a look how this is implemented so event user let's copy that here to registered event registered i will try i will try to auto complete that registered event and here and then we have event user and then we can fire notification so notify new welcome email notification so that will be fired whenever the registered event is fired so in here so wherever else you want that email to be fired you just create for example i will just copy and paste it somewhere login controller or whatever public function something you just fire the event of registered with user as a parameter so you create that user wherever and this is how you fire that event and that event would be listened by welcome email listener and send a notification let's try it out let's remove the observer so an app service provider let's comment out that observer and let's register again okay registration fake filler and let's see if we have an email and we have an email a few seconds ago which means that it comes from the listener and by the way that event and listener logic is not only in laravel ui it's in core laravel even if you use jet stream or fortify there's still an event service provider that has that protected listen with registered class and events so the same logic i've showed with events on listeners it comes from the core laravel whichever version you use so i hope it was helpful if you want to receive more videos on laravel subscribe to the channel and also you may support my channel financially by doing one of two things using our laravel quick admin panel or enroll in one of my laravel courses currently i have 12 and this october i've celebrated a milestone not sure when you're watching this video but in october 2020 a record 3.5 000 revenue that's pre-taxes so in fact i will probably earn something like 2 000 or 2 point something but still it could be a full-time income already so if you want to support me with that and then i will have more time to shoot free videos on youtube choose one of the courses unteachable and see you guys in other videos
Info
Channel: Laravel Daily
Views: 20,522
Rating: undefined out of 5
Keywords:
Id: ZWzH6SOzjhI
Channel Id: undefined
Length: 9min 36sec (576 seconds)
Published: Mon Dec 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.