Advanced Laravel | What is Laravel Events & Why and How Use Events #16

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so what is event and listener and how we can use these things and even why they are present so we will get the answer of these questions in this episode so let's start the video and welcome back to balloons I am your host Sadiq and this is a channel that I talk about lateral and the cool thing about levels so if you are new to this channel don't forget films on Facebook Twitter and Instagram so what we have we have event what our event actually you can think about event as any task which is implemented in your application can be think about as event and whenever that event happens that means that task is we started then there must be something which can listen about that you can think about this like you are shouting for hey I have completed this level project and if nobody is there to listen you then why you are shouting so events are just like you are shouting for any task and listener exactly they listen the events so so even shout hey I have a task so is there anybody who can listen about that and then the listener comes and yeah I am so I will work about this task so this is the event and the listener but why we have it here we can do all these things without these events you can see the level is very very smart and it provides you these kind of things so that you can implement this thing in your bigger project when you have lots project there are many tasks you are repeating every time so is it's a very good idea to separate out all these events or the tasks in a new event and make a listener and do the work listen and this event and listener is very very very useful when you are broadcasting and creating some kind of notifications so this is the thing and let us see how we can use this so I have this fresh installation of level actually it's not a fresh the Q video I have done before that is on this project so you can see whenever I go to app and then providers and event service provider you can see we have this event service provider which extends service provider ok so that's okay but we have to go here you can see it's saying protected listener so first is this level provide these two one is event and the listener so you can make this like task event okay and now I am giving this task event listener Oh listener okay so I have given a task event that means whenever my task event is happening I will give that task to this task event and whenever this task even get any task then it will shout and the listener of this task will get that work done and the important thing is that you can make as many listeners for a single event as you want but we will provide only one and just after this because we have given all these things here by default and now we have to run one single command so I will go here and I will open another command prompt and I will say here PHP artisan event and generate you can see this command will generate the event and the listener also if you want you can separately create the event separately create the listener but that not I recommend that's not level recommend but this is very very useful because you can see here we don't have any event folder here we don't have any listener folder so when I go and hit enter it says event in Lipner generated successfully and when I go here now we have events folder and listeners folder and as we have provided the name of event at task event you can see the task event is here and as we have provided the listener as the task event listener the task event listener is here and most important thing is that we don't have to include our event in the listener you can see because levels know that this listener is for this event so that's why it's already implemented these things at the top okay so what we can do let's pass on this constructor of this event I am passing this arrow message let's say message is equal to I will say here dollar message and where this dollar message came from that is from this constructor and we have to make a public variable called dollar message so that I can pass any message to this event okay so here we have the event and let's open the listener listener is here so when this event happens that event get everything we are passing on the event and then pass everything to listener so in the handle function of this listener you can see we have this task event and the event so let us return event for now so that we can see what we are getting and we have this listener we have the start we just now have to dispatch the event so let's just open web dot PHP file and let's create a route of full get method and I will say here let's say event function and then I will say here we have to dispatch the event so I will say event inside this new the event name is the event name we have created task event and we have to pass the message here so I will say hey how are you ok so we have to include this at top but if it is task event not task okay so let's just find this at the top and we have used HP companion ok so that's good and we have this listener and and we have passed the message in the task event that message will go here and we are getting that message on the constructor passing it to this - event and event is passing everything to listener listener is listening and BR returning that event whatever it is so let's just go to that event routes slash event hit enter it's not doing anything okay so do one thing on the listener instead of returning this task we will die in term okay refresh it one more time and yeah now we can see the message we are passing is hey how are you I am fine okay so in this way we can use the is listener event and listener and in the next episode we are actually getting what we can do with that we have just seen that how it work but where we are going to use this event and listener and all these things in the next episode so if you have any doubt feel free to comment me message me on Facebook Twitter and Instagram don't forget to like on these platforms support me on patreon calm and we will meet in the next episode ill then good bye
Info
Channel: Bitfumes
Views: 29,722
Rating: undefined out of 5
Keywords: laravel events, event laravel, laravel event, event and queue, laravel 5.4 event, laravel 5.5 events, events laravel 5.5, laravel event email, laravel email event, queue events, queue event
Id: e40_eal2DmM
Channel Id: undefined
Length: 8min 36sec (516 seconds)
Published: Thu Jul 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.