Listening to Eloquent Events0:00
So you can see here that I have a Post model. But what if we wanted to hook into when Laravel actually creates a new Post? Well, what you may not know is Eloquent will fire a series of events. So as such, we can listen to them anywhere in our system, a service provider, wherever you want. Now the syntax is going to be eloquent. the name of the event. So how about created? When a new, in this case, Post is created in the database. And then specifically, I'm going to listen on the App\Post model. Now let me show you how that gets fired. So if we look for, what is it, fireModelEvent. This is in Laravel's HasEvents class. Yeah, if we scroll down, here's the event that gets fired. Eloquent. the event, like created or saved or deleted, and then the name of the class. Okay, so let's just listen for that. And var_dump, we caught when a Post was created. Okay, let's give it a shot. php artisan tinker. And we'll build up a new Post. So I'm going to
Mapping Events to Classes0:49
Okay, so let's just listen for that. And var_dump, we caught when a Post was created. Okay, let's give it a shot. php artisan tinker. And we'll build up a new Post. So I'm going to use forceCreate here so that we don't get any mass assignment errors, where the title is some title and the body is lorem ipsum. And there we go. You can see that we did catch that event. Okay, so that's useful. But now the new part in Laravel 5.4 is we can map these events to dedicated event classes like this. I can now say protected $events. And I'm just going to give it an array of event names, such as created, which we just used, and then a typical event path. And I think you'll find this is especially useful, where maybe when a Post is created, you immediately want to fire an event called PostWasPublished, right? But maybe in the past, you would manually trigger that event. Well, another option is just hook into the created event. Okay, so let's go ahead and
Generating Event and Listener1:37
fire an event called PostWasPublished, right? But maybe in the past, you would manually trigger that event. Well, another option is just hook into the created event. Okay, so let's go ahead and create this event first. So I'm going to go into app/Providers/EventServiceProvider.php, and we're going to say these classes don't exist yet. But we'll say, when a PostWasPublished, I would like to trigger a listener called NotifyBlogSubscribers. Alright, so because I've updated this, I can now run php artisan event:generate. And Laravel is going to build up both the event and the listener. So you can see those right here, as well as here. Okay, so now we're just going to map the created event to the PostWasPublished class right here. Let's give it a shot. If we switch over, when we NotifyBlogSubscribers, yeah, you would send an email. And we'll say I am sending an email to the blog subscribers about this post.
Passing Model to Event2:27
If we switch over, when we notify our blog subscribers, yeah, you would send an email. And we'll say I am sending an email to the blog subscribers about this post. And yeah, let's give it a shot. So we can now import the event, and then go back to our routes file and get rid of that dummy code. So now we boot up php artisan tinker, we force create a Post. And sure enough, now we're using a dedicated event class, as well as a listener to catch that and perform any kind of logic you need. Now don't forget, you can still access the model associated with the event. So let's just var_dump the event post, and then we'll just cast it to an array. So now if you want to accept the model, as always, your event class just has to, well, accept it. So we're going to have our post here, push this below, and we'll make it public. Okay, now we should get the exact same thing. Create the post. And sure enough,
Reviewing Available Events3:12
well, accept it. So we're going to have our Post here, push this below, and we'll make it public. Okay, now we should get the exact same thing. Create the Post. And sure enough, we did get access to that data. So now in closing, when it comes to the events you can use, you have everything from created, saved, deleted, restored. And I believe there is a what is it called getObservableEvents. Yeah, so here's all of the various events that you could listen for, and map them to dedicated event classes.
