Playing
01. Grouping Laravel Notifications

Episodes

0%
Your progress
  • Total: 8m
  • Played: 0m
  • Remaining: 8m
Join or sign in to track your progress

Transcript

00:00
In this snippet, we're going to be looking at how we can more elegantly send a notification to a group of users. We're first of all going to look at how we would normally do this, and then we're going to just modify things very slightly to make this a little bit more elegant.
00:16
So as an example, I have some users just over in the database here, and I already have a notification set up, which I've just pulled the namespace in for here. And this is just a comment reply. So it's a really basic mail-based notification, and it just goes ahead and generates some markdown email, and that's pretty much it. But of course, this will work for all
00:34
notification types. Now, just open here, I've got MailTrap. I already have everything set up, so any email that I deliver will be sent over to here. And we're going to go ahead and look at how we would do this. So this isn't really a problem. It's not the most ugliest thing, but normally what we would do is do some kind of each loop over our users, getting in each of them users. And then for
00:56
this user, we would notify them with a new comment reply or whatever the notification happened to be. That's going to go ahead and work absolutely fine. So if we just give that a refresh, that's going to take a little while because I don't have queuing set up. And sure enough, we will get these emails delivered through to our platform here, our MailTrap platform. Okay, so let's take
01:17
a look at how we might take this and make it look a little bit cleaner. First of all, let's just see how we would want this to look. So I might want to say on this group of users, I want to notify them with a new comment reply. That would be a little bit more elegant. And of course, you can use any other techniques in this snippet to do a very similar thing because we're going to be
01:36
looking at custom Laravel collections. So of course, if I just head over to the browser and give this a refresh, you would expect that we have a notify method missing on a collection. Of course, that is the case. Now, if you're not aware, if we just come over to the user model here, what you can actually do is you can return a custom collection within a model. So when you grab
01:59
a load of records out of the database, what you can actually do is return a new collection from here. Now, we can get this from the model. So this extends the base user as part of the Laravel framework. So let's just go ahead and open this up in here. And you can see that this extends our model. So inside of our model, we have a method in here called new collection.
02:23
What this will do is it will return a plain collection of all of the models that have been fetched out of the database. So what we can actually do is take this method, and we can pop it inside of our user model just inside of here. And what we can then do is return a custom collection. So the first thing that then is to create a custom collection, and then that collection
02:44
will have on it that notify method. So just over here, I'm going to create a collections folder. Of course, you can place this wherever you want. And I'm going to create a custom user collection just inside of here. So this user collection, if we just build this class out very quickly, will extend Laravel's base collections. We'll need all of the basic functionality
03:05
from a collection on this. So let's pull the namespace in for this and pull the namespace in for illuminate support collection or illuminate support database collection. So now that we've done this, we can go ahead and create that notify method on here. So on that collection, we can now go ahead and call this method. Now, just over in here, let's switch the
03:26
notification over or the collection, sorry, over to user collection, keeping the signature of the method exactly the same. So let's pull that in. So now if we head over here, what we should see is notify died and dumped. And inside of our collection, we now have access to this items, which will give us the collection of users that we're trying to notify. So we could do everything
03:49
in here. But what we're going to do is split this out into a new class. So if we just go over to notifications, let's create a features folder in here to keep this separate. And let's create out a group notification class in here. And we'll just build this out. So this is group notification. And let's pull the namespace in for this at the top. Okay, so into our group notification,
04:13
what we want to take in it are the notifiable things that we are working on. So from this notify method, what we can do is return a new group notification, just to keep things nice and tidy and separate rather than doing everything within this method. And as a dependency to this class, we can pass the items in and the notification that we're trying to send, which of course we can
04:39
pass into this method, because the way that we set this up is we pass the notification in. So let's take in the notification. And we'll go ahead and type in that. So we make sure that we're actually getting a Laravel notification in here. And we can go ahead and pass that through. So over in the group notification, now we can accept this into the constructor. So let's go
04:58
ahead and in here, we expect to find a array of notifiables, I'm going to call these and a notification as well to pull that in and type in that as well and say notification. So let's pull the namespace in for notification here. And we are done. So let's just die dump on our notifiables just to make sure that that's being passed through properly. And of course, over in the collection
05:24
here, just make sure that we pull the namespace in for that. So that will give us exactly the same thing. So we're just passing them through to this group notification. And of course, the same for the notification itself, we should just get a comment reply. Okay, so now what we want to do is assign these two notifiables, but we want to collect these up because they're currently as an
05:44
array. So we're going to pull them into a collection. And then we're going to set the notification as we normally would into here as well. So we can send that off. So let's just create a couple of properties just up here. So notifiables and also that notification. So now what we can do from this is go ahead and send this notification. But what we don't really
06:07
want to do is chain on another method in here to send this or dispatch whatever you want to call it. So what we're actually going to do is we're going to take advantage of a PHP magic method destruct. This is called when there are no more references to this particular class. So if I just died up here on send, that will send off that notification when we've got no
06:27
more references to this. So if we just go and give that a refresh now, you can see we've got send here. This is a really useful magic method for doing something when you don't want to call an additional method. You can create an additional method, it doesn't really matter. But we may as well make use of the destruct magic method here. So now what we want to do for this is for each of
06:46
the notifiables, we're going to go ahead and use the each method because remember, that's now a collection. And inside of here, we'll get each of them notifiable things. And of course, that is, in this case, a user. Now what we can do with this is we can go ahead and actually notify this user. So let's extract this out to another method called notify. So let's say notify and pass through
07:09
the notifiable. We don't need to pass through the notification because we already have this stored in this class. So let's create that notify method in here, taking through the notifiable, which again is the user. And now all we need to do is say notifiable, notify, and then just say this notification. So essentially, we're just doing exactly what we were doing normally inside of a
07:32
loop. But we're now doing it inside of here. So let's just take a look at how this works. Let's go and give this a refresh. Okay, so we've got an error here that just needs to be this notification and give that a refresh. And we can see a slight delay. And we should start getting them new emails through. And that looks like it's working great. Now, if you wanted to delay this notification,
07:53
it's just as easy as going ahead and passing the delay option to the actual notification you're creating. So you'd want to wrap that first of all, I'm just passing in the carbon instance for the current date and time just at the moment. And if I give that a refresh, sure enough, that will be delayed, although at this point, it won't be. So there we go, a really quick way to create a custom
08:16
user collection, create a group notification class, which takes in the users or any other object that you want to notify, the notification itself, and then iterates through and notifies the group of users. If you're doing this kind of thing a lot, this is going to save you a few lines of code every single time.
1 episode 8 mins

Overview

Need to send a notification to a group of users without looping in your controllers? Let's abstract this away and write some nice clean code.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!