Events In Laravel | How To Use Events | Listeners In Laravel | How to add Newsletters

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up developers that's dari here and welcome back to a new video where we're going to talk about events in laravel quick pause do you want to support the channel and want me to continue on pushing content well you can support the channel through patreon right now you will also get benefits just as a private discord group where everyone is helping each other out with their coding issues so if you are interested to join the link will be in the description down below in the last video which i will link down below we've talked about how to use jobs and queues in laravel let's rewind a job is basically a trigger that will tell your application that something needs to be done but what if you want to be triggered whenever something has happened that's when you will be using events events are basically notifications that something has taken place let's say that you have an input field where a user needs to enter his email and he can submit it by clicking on the submit button to subscribe to your newsletter you basically want to get notified right therefore you can use events every developer has worked with events before even if you're not aware of it since some events are even triggered by the framework itself whenever you make use of let's say eloquent models you're basically going to trigger an event to create read update or delete something before we dive into the code to fire an event let's actually create a static page where we're asking for an email address of a user and a submit button i'm once again using my github repository which is a static larval project with tailwind included first thing that we need to do is to change our endpoint because i don't want to work with a page like this let's navigate to the cli because we need a controller to process the view so in here let's generate a new controller by performing php artisan make colon controller space a file called news leather controller let's hit enter let's navigate back to visual studio code let's open our app folder http controllers where our newsletter controller will be located now let me actually zoom in today's video is brought to you by cloudways a managed hosting platform for your applications cloudways has enabled businesses to scale and reach new milestones instead of worrying about server hassles with reliable 24 7 support cloudways offers complete peace of mind from surfer level nightmares furthermore its superior tools and works enhance your productivity so you can focus on creating incredible experiences with your applications it's a fast managed solution for agencies smbs and e-commerce businesses what i personally like about gladways is their slogan moving dreams forward we do need to set up a new method right here which will basically return the view to our index so let's create a new method called public function index and we're basically going to return a view with in single quotes a name of index there are two more things that we need to do we obviously need to create the index.blade.php file and we need to set up the route so let's start off with the index.php file let's open resources views and right here we're going to create a new file called index.blade dot php and let's just write down newsletter to double check it but before we can test it out in the browser we need to define our route inside the routes folder the web.php file let's get rid of the route that we have and let's create a new one called route con column get which accepts two params the first one is the endpoint so in single quotes forward slash comma brackets the controller that we want to associate it with so newsletter controller column column class comma and single quotes the method which is index if we save it and navigate to the browser refresh it you'll see that the method does not exist so i probably haven't saved the file let's test it again alright my bad newsletter has been printed out so let's navigate back to design our index page real quick with tailwind like i've said i've already implemented tailwind in this project so let's go to our index.blade remove newsletter first things first let's write down doc and hit tab to get our html template now inside the head tag we do need to link our tailwind css file so right below the title let's write down a link hit tab inside the href let's add a double set of kerdi braces we're going to call the mix function which will look inside the public folder where our css file is located in single quotes css forward slash app.css i don't like to work with ugly designs so let's quickly build something that looks pretty nice inside our body tag let's add a class right here of bg dash gray dash 200 the width is dash full the height is full and the font is dash sans let's go inside our body and create a new div which will be our parent element that needs a class of flex and h-screen then in here we're going to create a child element which is also a div and we're going to add a class of m auto inside our child element we can create an h1 with the name of newsletter i mean the text of newsletter excuse me and let's add a class of text as center the pb is 12 so the padding bottom the text is to excel and the font is bold now let's save it and let's go to the browser to double check it all right it's nothing fancy below our h1 let's create a form with a action to forward slash subscribe and we need to add a method which is post inside our form let's add a csrf token so a cross-site request forgery token and right below we need one input field with a type of text and let me actually outline this on the line below all right let's give it a name of email it has a placeholder of enter email and right below our placeholder we need to add a class of pxs4 the py is 2 we have a shadow excel we have rounded excel the placeholder dash gray-50 is column colon placeholder if we save it and test it out inside the browser you'll see a pretty good looking input field even though it's coming from me but we still need to add a button so right below our input field let's add a button give it a class of bg blue dash 500 the hover will change the color to bg blue 700 the text is white the font is bold py is 2 p x is 4 the margin left is 4 and it's rounded dash full now we obviously need to add a type which is submit inside the button let's add the text of submit all right i can work with this as we have defined in the action of our form right here we're going to call the subscribe method so let's define our route first and then move on to the controller so if we open our web.php file right below our first route define a new route call a collin post we're not going to get something but we're going to post something the endpoint will be forward slash subscribe and then we're going to add a comma brackets and we're going to call the newsletter controller calling column class comma method is subscribe there's actually one step that we have forgot and that's adding our migration because we do need to save the email address of a user inside the database so let's navigate to the cli let's perform a php artisan make a migration called newsletter hit enter alright our migration has been created let's navigate back to visual studio code open the database folder migration and it's the last one it is a class of newsletter so inside the up method let's create a schema column column create inside the create method we're going to define the table name which will be newsletter comma function parentheses inside the function we do need to pass in the blueprint variable table and inside the schema we have our table id we have our table string which is the email and the last one is the table timestamps save it close off the file because we're done with it but we do need to migrate it inside the cli let's perform a php artisan migrate as you can see our migration has been created so let's actually test it out let's go to my sql now let's say use larval underscore db that's desk newsletter as you could see our id email created at and updated at have all been created now the next step is to navigate to our newsletter controller and do something with the method that we have mentioned inside our web.php file so if we go to our newsletter controller right below our public function index create a new public function called subscribe now just like the jobs controllers migrations models and you can go on we can create an event through the cli by performing the php artisan command again so let's navigate to the cli let me navigate to the other tab and let's say php artisan make me a event called user subscribed what this command will do is basically creating a new folder inside the app folder let me show it to you right here called events and if we open it you'll see a user subscribed file in here next to the event there's one more found that we need to add which i haven't mentioned up until this point which is a listener it's not necessary to have one but i prefer to work with the listener as well since if someone subscribes to your newsletter you do somehow want to notify them that the user has subscribed right you want to return something back to the user and that's what you can do with a listener so let's navigate to the cli one more time let's perform a php artisan make me a listener called email owner about subscription hit enter our listener has been created but before we can use our listener and event we do need to register it but i want to wait and do that at the end of the video so you can see what will happen if we don't register it we won't be focusing on the listener right now but we will start with the event so let's open it this file has something that you'll see throughout most of the files such as a namespace some use statements a class name and some trades right here what the dispatchable trade will do is basically give it a method to dispatch itself a trade that we haven't seen before is interacts with sockets and what this basically will do is broadcast with the broadcast on method that we have down here and i think that i have mentioned serialized models quite a lot on this channel so i won't dive into it again the goal is that this file the user subscribed lets us know if an event has happened in our case a user will subscribe with their email address so we obviously need to do something with the email that is being passed in right above our constructor and right below our trades let's create a property public email what we then can do inside the constructor is obviously pass it in as a param so email we do need to do something with the object that will represent the event so in our case we need to say that this email is equal to variable email so the email property now the next step is to do something inside the controller and specifically doing something with the event that we just created so let's save it navigate to the newsletter controller now before we do something i usually like to add a dd here to see if everything works fine so let's go back to the browser so test at gmail.com click on submit and as you can see ok has been printed out now let's navigate back to visual studio code let's open our newsletter controller remove the dd the first thing that we need to do is to validate our requests and i want to mention it one more time that you should never forget that you're working on saving data in the database even though it's a simple input field that we have before we can validate it we do need to pass it in inside the param so request variable request then inside our method we could basically say that well get me the object validate it and let's pass in a set of brackets right here we're going to work with a key value pair so the key will be email and the value will be required pipe unique colon the table so newsletter comma the column which will be email now there are multiple ways on how you could handle or fire an event i prefer to use the event methods so let's define that right below request validate let's say event parentheses semicolon inside the event method we're going to pass in a object of the user subscribed so a new user subscribed what we have said inside the controller is that the user subscribe does need to receive an email so what we could do is to say get me the request input email we're basically passing in the email that we're getting from the input field what we could do then right below our event is to basically say well return me back to the screen if we save it navigate to the browser refresh the page enter the test at gmail email again hit subscribe you'll see that the page will be refreshed but nothing in particular has happened since we haven't defined anything that needs to happen when a user subscribes that's the thing that we need to do with the listener if we navigate back to visual studio code and open our listener folder where we have our email owner about subscription class scroll down to the handle method because the rest is actually not something that we need right here now what the handle method basically will do is expecting an event and in our case it will be the user subscribed so right below variable event let's say user subscribed hit enter save it all right let's remove the comment because we need to do two things right here we need to save the email inside the database and we also need to send a user that just registered a confirmation that they have been registered i'm not going to use eloquent right here but just a database query and in order to do that we need to call the db column colon table well the table name is obviously newsletter i've got an error right here because we need to import the fae gate alright right below my table method we're going to chain another method called insert let's go inside our insert method add brackets and hit enter we're going to add a key value right here again the key will in single quotes be email and the value will be grabbed from the event email now that was it for the query which was pretty simple then if we go right below our query we're going to call the mail vacate so let's pull it in again column column two the two method accepts one param which is the receiver which will be variable event email we need to chain one more method here so let's say sent because we're going to send it to someone we don't have a mail class so let's create it first before we continue on let's navigate to the cli let's perform a php artisan make me a mail command called user subscribed message hit enter let's navigate back to visual studio code let's open our mail folder use a subscribe message scroll down to the build method and let's change the view to mill dot subscribed we obviously need to create it so let's do that as well let's open the resource folder views create a new folder called mail which will refer to the mail right here and then the subscribed.blade.php file needs to be created as well so let's say subscribed.blade.php i'm not going to make a complex design right here so let's add an h1 with a text of user has subscribed save it close off the file we don't need our mail as well so save it again close it off inside the send method of our listener we're going to pass in the new object of the user subscribed message if we save it navigate back to the browser refresh the page enter our test at gmail.com email subscribe you'll see that we're getting a type of response because the input field has refreshed let's navigate to the cli to the my sql tab let's select everything from newsletter you'll see that nothing has been added right here because remember i've told you in the beginning of the video that we do need to register our event and listener for that a larval comes with a predefined file that we can adjust if we navigate back to visual studio code you'll see inside the app folder there will be a providers folder and if we open it you'll see a specific file called event service provider let's open it obviously this file corresponds with the events that we're working on in this video inside the property list that we have right here called listen we need to define the event and the associated listener after our registered class let's hit enter let's call the class of our event called user subscribed column colon class and we're going to associate it with a set of brackets and in here we're going to call the listener which will be email owner about subscription column calling class and this should do the work if we save it navigate back to the browser let me actually open mil trap let me go to my dashboard and as you can see my dashboard is empty so let's refresh my localhost add an email of test gmail.com let's click on submit that took one second which is alright let's go to miltrap and as you can see user subscribed message has been received if we then navigate back to item let's hit the arrow up to select everything from newsletter again and as you can see we have added one new row right here 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: 2,481
Rating: 4.9661016 out of 5
Keywords: laravel, laravel 8, laravel 8 tutorial, laravel 8 advanced, php laravel youtube, learn laravel step by step, laravel tutorial youtube, learn laravel 8, events in laravel, how to use events laravel, what are events, event listener laravel, event listener laravel 8, laravel queue event listener, laravel newsletter system, how to add newsletter in laravel, queues laravel, laravel listener example, laravel 8 events and listeners, learn laravel events, learn laravel listeners
Id: aq2E1oaksag
Channel Id: undefined
Length: 19min 48sec (1188 seconds)
Published: Mon Jul 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.