Introducing Notifications 0:00Now that you understand the basics of sending email, let's switch over to another piece of the puzzle, which is notifications, and we'll talk about when to use each. Okay, so if I run phpArtisan, I've actually whipped up a fresh, empty project, just to keep things nice and simple. Anyways, if I scroll up to the make command, yes, we can create a new email class. But we could also do a notification class. Okay, so let's just dig into it and see what we can do here. So if we run the help command, we can see all we need is the name of the class. So like an email, a notification can be used to represent some kind of action that took place for the user in our system.So like an email, a notification can be used to represent some kind of action that took place for the user in our system. And this should be fairly self-explanatory. You use any kind of site, you take an action, and then all of a sudden you get an email. Or you get a text message asking you to confirm something. Or a notification that your last payment failed. Any kind of basic notification would make sense. So you'll notice that I said, well, we email the user, or we text the user. Or maybe you notify them on Slack. Or maybe you notify them directly within your web app.Or maybe you notify them on Slack. Or maybe you notify them directly within your web app. Maybe you click on a little bell next to the user's profile, and that pops out all of their notifications. So what we can see is, for any given notification, you could distribute that in multiple ways. For example, for Laracast, maybe a notification we might send is that your subscription renewal failed. OK, well maybe I text you, maybe I email you, maybe I add a note to your notifications panel. Maybe we use a different channel entirely. Let's give that a shot. Creating Notification Class 1:34Maybe we use a different channel entirely. Let's give that a shot. I'm going to make a notification for subscription renewal failed. Now this will be stored within your app directory, notifications. OK, so if we take a look at it, we're going to see, on this API, a method called via, and then these methods called toMail and toArray. And there's actually, I think, four or five of them. So via is an important one. What are the delivery channels for this particular notification? And like I said, it could be a text message.What are the delivery channels for this particular notification? And like I said, it could be a text message. It could be a phone call, even, if you want. It could be an email. It could be just throw it into the database. You can use a bunch here. Let's refer to the documentation for a full list. In this case, we can see, by default, it's going to assume just a mail notification. Now for each of these channels, there will be a method like toMail or toDatabase or toSlack, if you use that channel.Now for each of these channels, there will be a method like toMail or toDatabase or toSlack, if you use that channel. These methods are responsible for preparing the notification according to that platform. So in this case, to send an email, you can see that Laravel provides a nice little API here to dynamically whip up the email. Or we could use the same Markdown mail that we learned about in the mail lesson. Take a look. If we go to this MailMessage class, and if we look for Markdown, there we go. We can use the same exact thing as you learned in the mail lesson. But often, for basic notifications, this little API is really great here. Sending Notifications to Users 2:54We can use the same exact thing as you learned in the mail lesson. But often, for basic notifications, this little API is really great here. So we're going to give this a shot. So let's take a look. If I were to new up a SubscriptionRenewalFailed instance, when it gets fired off, by default, it's going to send an email to the associated user, and that email will look like this. OK, so that's one piece of the puzzle. But next, if I open up our User class, you're going to see this section here, Use Notifiable. This is a trait that allows us to say, for any given user, notify them of the thing that just took place.This is a trait that allows us to say, for any given user, notify them of the thing that just took place. So in our case, that would be SubscriptionRenewalFailed. And what's cool about this is, when we were just reviewing basic email, well, we would say something like, mail to the given user, and then send them that email. However, with this form, we might send them an email, we might send them a text message, we might update Slack, we might, there's any number of community-provided channels. So maybe you send them a phone call. Maybe you even send them a postcard. Believe it or not, you can do that.Maybe you even send them a postcard. Believe it or not, you can do that. That's very neat. So this API here is available through the Notifiable trait. So if we take a look at that, here's a notify method that you can use. OK, so let's give this a shot. We're just going to do it inline here. So I'm going to go to my routes file, User, and we'll just say, find me, just give me the first person. I've seeded the users table with one record.the first person. I've seeded the users table with one record. Now I can say, User, notify them that their subscription renewal has failed. So I will import that, like so. Alright, and we'll say, return, done. OK, let's give it a shot. Now if we run this in the browser, we're going to get a 530 authentication required. And this would make sense if you think about it. We specified that for this particular notification, our main channel is mail. So we are going to send an email.We specified that for this particular notification, our main channel is mail. So we are going to send an email. But because this is a new project, if I open up my .env file, our mail setting is set to use SMTP, but I haven't yet configured that. I'm going to change it to log for now. OK, let's try it again. Give it a refresh. OK, and now that's done. Let's go to Telescope and review it. If I scroll down to, well, I could do mail, but let's do notifications.Let's go to Telescope and review it. If I scroll down to, well, I could do mail, but let's do notifications. Here it is. We take a look at it. Alright, so we fired off a notification, which triggered an email. So back to the Mail tab, and here is what we got. So even outside of the fact that we can now send users notifications in multiple ways, notice that we also populated an email just using a basic PHP API. In this example, we didn't even have to create a markdown file. You can do that if you need a lot of flexibility, and sometimes you will when it's a very intricateIn this example, we didn't even have to create a markdown file. You can do that if you need a lot of flexibility, and sometimes you will when it's a very intricate email. But often, again, for things like this, it's much simpler to take this approach. So notice the basic API, line, action, and line. So line will just be basically a paragraph, the intro to the notification, and action is an action you want the user to take, like a button. So we have notification action, and that has a URL to the home page of the site. And then finally, another line. And in fact, if you go to Mail message, and we can see, yes, you can still load a viewAnd then finally, another line. And in fact, if you go to Mail message, and we can see, yes, you can still load a view if you want, or a markdown-based view. You can attach data. You can... Ah, let's go to the parent class here. Anyways, you can do a line. If you want to specify that there was some kind of error, you can call that method. And I think all that's going to do is change the button color to red, or something like that.And I think all that's going to do is change the button color to red, or something like that. We can specify the subject. We can do a lot here. So in the case of subject, you'll notice, if we switch back to Safari, the subject of the email is SubscriptionRenewalFailed. So you'll see that it takes the class name, and it turns it into a sentence. So if you want to change that, yeah, here is where you would say, the subject is, whoops, your last payment failed. We'll say add a new credit card, or something like that.your last payment failed. We'll say add a new credit card, or something like that. You get the idea. Okay, so now, if we come back and give this another shot. All right, let's go back. We should now have a new email that was fired off, like so. And also notice the subject has been updated as well. Okay, but let's say, in this case, we want to fire off an email, but maybe we also want to notify them through the web app. Okay, let's add another channel. Storing Notifications in Database 7:35to notify them through the web app. Okay, let's add another channel. Yes, I want an email, but I also want to store it within the database. And that way, later, I can query it when the user returns to the website. I can just do a basic query to fetch all unread notifications. But now, if we give this a shot, I think it's going to fail. So let's run it. Yeah, it does. So it's trying to insert this notification into a notifications table, but we don't have one.So it's trying to insert this notification into a notifications table, but we don't have one. So as it turns out, for database-specific notifications, we need to create that table. And luckily, once again, Laravel will do the work for us. So there is a command called NotificationsTable that will create the migration for us. Okay, so let's run that. And then I will migrate my database. Okay, so now, if I switch back to SQL Pro, you'll see that we do have this NotificationsTable. And our structure is, what kind of notification is it? What is the ID of the person being notified and their type?And our structure is, what kind of notification is it? What is the ID of the person being notified and their type? So you'll see that it's not UserID, and that's because it is polymorphic, which means most of the time, you probably will say UserNotify. But you can add that notifiable trait anywhere. So let's say you have a Project class. Well, if I add the notifiable trait there, I can say Project, and then notify the owner. It's pretty neat. Anyways, that's the reason for that. We have the data, and then some basic timestamps.Anyways, that's the reason for that. We have the data, and then some basic timestamps. So if we give this another shot, think about what was done here. We have fired off an email to the user that their subscription renewal has failed. In real life, this would probably be in response to a webhook that you catch. Maybe Stripe pings a URL on your system when a payment fails to go through. You pick up on that, and you respond to it by firing off a subscription renewal failed notification. But anyways, yes, it sent the email, but it also updated this table in your database. So if I give it a refresh, here we go.But anyways, yes, it sent the email, but it also updated this table in your database. So if I give it a refresh, here we go. So we have our unique ID. Here is the instance of the notification, basically, the class name. And we are notifying the user with an ID of 1. And in this case, there is no data associated with it. But there could be. Next, you'll see readAt. This can be used to specify that the user has read the notification. So take a look. Reading and Marking Notifications 9:51This can be used to specify that the user has read the notification. So take a look. I'm going to boot up phpArtisan Tinker. And we will once again find the user. And we know that we can call notify. But there is also a relationship to notifications. So if I fetch all of the notifications for this user, in this case, they have exactly 1, the one we just fired off. So now you could have an endpoint in your app that fetches all of the notifications for a user, it filters through them, and it presents them within some kind of list.So now you could have an endpoint in your app that fetches all of the notifications for a user, it filters through them, and it presents them within some kind of list. You can even mark them as complete. So if I were to say, well, give me the first notification, or do a forEach, something like that. Now, on this instance, notice this is an instance of database notification. Anyways, I could mark that one as read. So maybe when the user clicks on it, we would submit an AJAX request. Or maybe when they view any of their notifications, we will mark all of them as read on the fly. Anyways, if we now do it again, you'll see that readAt has been populated with the currentOr maybe when they view any of their notifications, we will mark all of them as read on the fly. Anyways, if we now do it again, you'll see that readAt has been populated with the current timestamp. So now you're thinking, well, what if I only want to fetch the unread notifications for the user? At the moment, this is going to return all of them. Okay, well, as you can imagine, there's an unread notifications. And in this case, we don't have any at the moment. So I don't mean to overwhelm you. Of course, for these things, go to the documentation, you can follow the steps when you need to.So I don't mean to overwhelm you. Of course, for these things, go to the documentation, you can follow the steps when you need to. It's not like you have to commit all of this to memory. But the point is, you have a lot of flexibility here. So when you hear this concept of a Laravel notification, think of it as a way to alert the user about some action that took place in your system that they need to know about. Now when it comes to how you notify them, that's entirely up to you. Again, you might send them an email, you might send them a text message, you might just throw it in your database, or you might do all three at once. And that's what's really cool about this. Mail vs Notifications 11:46it in your database, or you might do all three at once. And that's what's really cool about this. You can have a single notification class that defines exactly how we will notify the user. So now your question is, well, when do I use a traditional email versus a notification? And I agree, that can be a little confusing at first. My advice would be, again, just base it on what is taking place. So if it is something like, well, this happened, so I need to notify the user, then use a notification class. If it's for something else, like a newsletter type thing, maybe you're running a sale, something different like that, then use a traditional mail class.If it's for something else, like a newsletter type thing, maybe you're running a sale, something different like that, then use a traditional mail class. Now finally, in closing, just do note, for every channel you specify here, and you can review all of them on the documentation website, you will have a corresponding method. So toMail, or toDatabase, or database falls back to toArray. So you'll see here, this array is empty, which is why the data section is empty as well. If you were to set this to foo var, then that array would be stored as a stringified JSON within this column here. So this can be useful for any additional metadata that you want to include, even if there's a basic message or something like that.So this can be useful for any additional metadata that you want to include, even if there's a basic message or something like that. Anything you want there would be fine. But yeah, you could also do toDatabase. You can broadcast notifications. There are so many different things you can do here that I don't want to overwhelm you. For now, know that when you need to contact a user, you can use basic email or you can use notifications. So with all of that in mind, if we rewind a few lessons ago, where we wanted to notify the user that a project has now been created, well, with this new education, I think youSo with all of that in mind, if we rewind a few lessons ago, where we wanted to notify the user that a project has now been created, well, with this new education, I think you might agree a notification class might just be a little more appropriate than firing off a general email.