If you've ever created a model observer in Laravel,
00:02
you'll know that you'll need to register these before they work. In this video, I'm going to show you a trait that we can build up, which
00:09
we can use on any model, which will automatically register an observer you're using. So first things first, we're just going to work with the base user model here.
00:19
I'm going to go ahead and create out an observer just so we can get started with this. So of course, I'm going to call this user observer. And let's go ahead and pass in that model as well,
00:27
just so we get some of the scaffolding that Laravel provides. So if we open up that user observer, over in created, let's just die dump something out in here and just say
00:36
created, just so we can see that it works. If we head over to the event service provider here, we can go ahead and put this in boot. So we can use the user model.
00:45
We can say observe. And then we can pass in the user observer. And that's how we would typically do that. We would come over here and do that.
00:53
Or you could put it in the observer property of this event service provider. OK, now we've done that. Let's go ahead and open up a tinker session.
01:01
And let's say user factory and create. And as we would expect, we get die dumped here, created. So we know that our observer is working. What we're going to do is get rid of this
01:11
and then build out that trait, which will allow us to use that directly on the user model. And then any time we build an observer, we'll just have to use this observable trait inside of here.
01:21
And everything will be registered for us. So let's figure out where we're going to put this. We just put this inside of models because it kind of relates to models.
01:29
And let's build out this observable trait in here. And we'll go ahead and just start to fill this in. So let's pop in the namespace. That's under models and traits.
01:38
And of course, let's start to build this trait out, observable. OK, so to start this trait out, we're going to go ahead and create out a public static function
01:46
called boot and then the name of the trait. So we're going to say boot observable. And what that's going to do is when this is registered, this will go ahead and fire.
01:54
And we'll be able to do anything we need to in here. So to show you how this works, let's just go ahead and die dump booted here. And if we head over to the user model
02:03
and just say use observable, and then we head over to Tynker and we just, let's just exit out of here quickly and go back into Tynker. Let's say user factory and create.
02:17
And we should see booted. So that's basically allowing this to run as it's used on this model. So now that we've got this running on our model
02:26
as soon as this is booted up, what we want to do is find the observer, which we're going to assume is in this observers directory just here. Find the class name of the model we're working with
02:36
and then just apply this into here with the prefix observer and then register this. So let's build up the path to the observer first of all. So this is going to be app and observers.
02:49
And then it's going to be x observer like so. So that's going to be the full path name to that. Now to get this, all we need to do is concatenate on and we can use the class base name function
03:01
which is a Laravel helper. And we can basically grab the fully qualified class name statically from this. So again, let's just die dump on the observer
03:10
and just see what we have here. Again, let's just exit out of Tynker, come back into here and let's run user factory create. And there we go.
03:18
So we've got app observers, user observer. That is the observer that we want to register for this particular model. Now to do that, we go ahead and we say new static.
03:27
So we knew up the user model in general and we use the register observer method which is over on that model. And we just pass the name of the observer in there.
03:36
And that is it. So let's go ahead and just try this out. And then we need to make sure that we catch here if there is no observer, because obviously
03:43
if we don't have an observer registered for a model that we're trying to apply the trait on, it's just isn't going to be able to register it properly. So again, I'm just going to exit out of Tynker here
03:52
and let's go and just rerun this and say user factory create. And there we go, we get created. So although we're not registering that over here
04:02
what this is now doing is registering this via that observer based on the user model class name. Now, really quickly, before we finish up it's really important to check
04:11
if we don't have this observer, if it doesn't exist. So we can go ahead and use the class exists function to check if that observer exists or not. And if it doesn't, we simply return and don't do anything.
04:23
So we don't actually register this observer. So there we go. Now, whenever we create a observer for a particular model all we need to do now is just use this observable trait.
04:34
We don't need to go over to event service provider to register this. We don't need to register anywhere else. We just use the observable trait and it's registered for us.
1 episode• 4 mins•2 years ago
Overview
If you're using Eloquent model observers, you'll know registering them can break your flow. How about a convenient trait to automatically hook up the associated observer? Let's build it!