How To Send Notifications In Laravel | What Are Notification | Laravel Authentication & Mailing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up laravel developers it's dari here and i hope that you're having a great day in this video i want to focus on notifications in laravel now before i continue on with the video i want to quickly let you know that you can support the channel through patreon which is linked in the description down below you will get some pretty cool benefits such as a private discord group where everyone is helping each other out and you can decide the video series that i'm going to make through pulse so if you are interested to join the link is in the description down below in web applications a very important topic is notifications if we imagine an app where we are going to notify students that they are allowed to enroll for a new course we should look at it as a relational way one notification will be sent to many students so we're going to create a one-to-many notification channel just like with sending emails where you perform an artisan command to pull in a phpml class you can do the same exact thing for notifications so let's navigate to the cli and here we need to create our first notification class so let's say php artisan make me something called a notification it accepts one argument which will be the name of your class just like anything else encoding keep it related to the task that you're going to create so let's call it test enrollment if we hit enter a notification folder will be created so if we go to visual studio code you can see that inside our app folder there's a notifications folder with one file a php class called test enrollment so let's open it if we take a look at the class we can see a lot of similarities with the mail class that we created in the last video but there are a few things different right there inside the constructor we're going to pass in relevant information that you want to send through the notification if we scroll down you can see a vaio method which accepts one param called notifiable and what this allows us to do is basically define in which notification channels we're going to use there's a two mail method right here and this is an individual method that is being used for every notification channel that allows us to specifically define how to send one of these notifications next to the fact that you can send out notifications you're also able to save them inside a database we're not going to cover that in this video but i will show you how you could easily generate a migration that is predefined for you let's navigate to the cli and in here let's perform php artisan notifications column table this command will generate a migration let's hit enter let's go to visual studio code let's open the last migration and we have a new schema called notifications now if we close it off and go back to our notification class so our test enrollment we're able to set everything up right now and we can focus on the write and controller after in order to pass in data inside the constructor we need to create a new property so let's do that let's say private variable enrollment data this private property that we have will receive data and it will be passed inside the constructor so let's do that let's say variable enrollment data inside the constructor we could get rid of the comments and say that this enrollment data is equal to the variable that has been passed in alright let's scroll down because inside the two mill method we're going to change up some things since we're receiving data through the constructor now let's get rid of everything that's in the line method let's say that we want to print out this enrollment data brackets body now let's also get rid of the string inside of the action now let's say that we want to print out this enrollment data bracket single quotes enrollment text we could also replace the url method right here so let's do that let's say this enrollment data and we want to pass in the url now as a thank you we could keep it static but let's change that up as well let's say that this enrollment data and we want to search for a thank you and this is pretty much everything that we need the next step is to set up our route and controller so let's do that let's save the file we are going to pass in data through the controller so let's go to the cli and let's perform a php artisan make me a controller called tests enrollment controller let's hit enter our controller has been created so let's go back to visual studio code and open it now our test enrollment controller has been created but before we do anything let's define our route first so let's open our web.php file right below all the routes that we have let me actually get rid of the odd error because the class hasn't been pulled in alright right at the bottom let's say that we want to create a new route with a get method we want the endpoint to be forward slash send dash test enrollment we're also going to pass in a second param because we just created a controller that we want to pass in so let's add brackets because we're going to pass in two params the first one is the class name so we just created tests enrollment controller column column class comma single quotes because we're going to pass in the method so let's say that the method that we're going to create in a second is called sent test notification save it close it off and we're ready to focus on the core logic inside our controller so let's open our test enrollment controller let's get rid of the comments let's create a public function send test notification inside the notification class we defined a property called enrollment data that will contain specific data that we want to send to a user so let's redefine it right here so let's say variable enrollment data is equal to an array let's go inside the array and hit enter what i usually do is to finding the keys first since we did that already in the notification class it must be easy for us right now first one is the body the second one is the enrollment text then we got the url and the last one was the thank you we are ready to find the values of our keys so for the body let's say you received a new test notification for the enrollment text let's say you are allowed to enroll now for the url let's just keep it equal to the url method that will redirect to the forward slash endpoint and as a thank you let's say you have 14 days to enroll before i continue on with notifying students on their new test enrollment i want to go over two different ways of sending a notification since you can either use the notification vacate or use the notifiable trade to an eloquent class which sends a notification to all users now let's start off by doing that let's start off with a notifiable trait by default every module that you create so let's open the user model you will see a use statement at the top right here which will pull in a notifiable trait that will be used inside the class this allows us to do something with a user inside the controller so what we need to do is to pull in the user inside the controller so right above our array let's say variable user is equal to user let's pull in the user model column colon first so get me the first value right below the array we need to send the notification via the trade and since we're using the notifiable inside the user model like i just explained we're going to use the two mail method right below our array let's say that variable user is notifying what we're going to notify is a new test enrollment which is the notifications class that we defined then here we're going to pass in variable enrollment data which will be used inside the constructor of the class save it in order to send the mail we need to go to the browser obviously let's go to larval let's change the endpoint to forward slash send dash test enrollment hit enter we have not defined a front end so the endpoint will be blank for us but i and you probably too have set up mel trap to catch emails so if we open mailtrap you can see that we receive one email called test enrollment which is a notification for the user besides the notifiable trait there's another way to send emails and that's through the notification vacate to be honest i prefer to use the trade but i will show you the notification vacate since it is something you will see a lot on the internet so let's go back to visual studio code let's comment out this line right here and on the line below we're going to use the notification vacate so let's say notification let's pull it in and be aware that you pull in the support vacate colin collin sent the first param is where we're going to send it to we grabbed up the first user so let's say variable user comma and then we're going to pass in the notification class so new test enrollment and in here we're going to pass in variable enrollment data now be aware that you pull in the right use statement the one that goes to illuminate support vacates and then to notification let's go back to the browser let's refresh our localhost for slash send test enrollment go to mailtrap and we just received a new email like i said both of them have advantages and disadvantages with the method that we just used we basically need to pass in the notifiable and the notification right here which is a little bit extra work but a huge advantage might be the fact that you could pass in more than one notifiable at the same time now this was it for this video if you do like my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit that subscribe button you
Info
Channel: Code With Dary
Views: 35,244
Rating: undefined out of 5
Keywords: laravel, laravel 8, laravel php framework tutorial - full course for beginners 2020, laravel 8 tutorial for beginners, laravel php framework tutorial full course for beginners, learn laravel for beginners, learn laravel step by step, laravel tutorial youtube, how to learn laravel, laravel tutorial 2020 - the complete developer course, complete laravel 8 course, learn laravel 8, notifications laravel 8, how to send notifications laravel, what are notifications laravel
Id: gtMXs9a1e0Y
Channel Id: undefined
Length: 10min 36sec (636 seconds)
Published: Mon May 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.