Notifications Overview0:00
Okay, so, far and away, one of the coolest new features in Laravel 5.3 is the Notifications component. So think of it like this. You can take any notification in your application. So for Laracast, one might be, a new lesson was published, right? Okay, so I can take that thing that occurred and distribute it, or notify the user, in multiple forms. So I can send them an email if I want. Or maybe they want a text message. Well, I can send them a text message.
Creating Email Notification0:48
that, and it's really, really simple. So let me show you. We're going to distribute this, distribute, we're going to separate this into a handful of lessons. So to start, I'm going to show you the most basic form, creating a notification that sends an email. And you'll see a few little fun things along the way in this lesson. Okay, so the basic shape will be something like this, user notify. So we can call a notify method on the User model, because in Laravel 5.3, it pulls in this Notifiable trait.
So we can call a notify method on the User model, because in Laravel 5.3, it pulls in this Notifiable trait. Now this comes out of the box, but the important thing to remember is, you can import this trait for any model. So if I want to call a notify method on my Post model, then all I must do is add this trait to the Post class. All right, so now how do we create a new notification? Well, of course, there's an artisan command for that. So let's use this php artisan make:notification called LessonPublished.
So let's use this Laracast concept. Make a notification called LessonPublished. All right, so now you'll see, if you run this the first time, you'll see a new notifications folder pop up, and within it is our class. Now if we take a look at this, yeah, a few new terms you need to learn, but it's actually really very, very simple. So we have different ways to basically translate the message for each channel. So what do I want the email version of this notification to look like? Or what do I want, of course, we can add more methods here. What do I want the text message version to be like?
Or what do I want, of course, we can add more methods here. What do I want the text message version to be like? Or the Slack version? So you can tailor the makeup for each of these. Okay, but anyways, in this case, we can see the default channel will be mail. So by default, we're going to send an email here. Now, what would be appropriate for a lesson being published? Well, maybe the lesson itself, the lesson instance. So why don't we add that here? I'm going to inject that.
Triggering Notification in Routes2:34
So why don't we add that here? I'm going to inject that. And then I can type in that and import it at the top. Yeah, so now in this case, I do have a User model and I do have a Lesson model. These are the most basic forms, so nothing you really need to know here. Other than that, I have a couple records in each table. So now I want to say, back to my routes/web.php file, I'm going to notify the User that a Lesson was published. So we can say new Lesson published, and I will import that at the top. Okay, but we did say a Lesson published requires the Lesson that was published.
So we can say new Lesson published, and I will import that at the top. Okay, but we did say a Lesson published requires the Lesson that was published. So let's do this. We don't have any real apps, so we'll just find the User. We'll say $user = app\User::first(). And then I'm going to do the exact same thing for the Lesson. All right, so we're going to sort of fake it. So give me the first User in the system, and we will notify them that the first Lesson in the lessons database table was published. All right, so that means we knew this up.
Building MailMessage Content3:24
that the first lesson in the database table was published. All right, so that means we knew this up. We pass in the lesson object. And now, yeah, we can use that information to prepare the email. So if we take a look at how we prepare this, it creates a new Mail message. But all these calls to line and action, and there's actually a few more, like subject, and we can set the mode, whether it's an error or success. So what's going on here? We'll think of this as a fluent API for rapidly building up an email. So we have rapid application development.
We'll think of this as a fluent API for rapidly building up an email. So we have rapid application development. I would call this rapid email development. So traditionally, you would do something like you would load a view, and you would say emails.lessonPublished. And then in there, you would create the HTML, and you would tailor it specifically for that email. But if you think about it, so often it turns out that most emails are the same. They have a greeting, they have a subject, and then they have some kind of call to action.
They have a greeting, they have a subject, and then they have some kind of call to action. So what this does is it encapsulates that entire process to make it easier on you. So why don't we do this? Let's fire this off, and then we'll dig into it a bit more to see what each of these does. But in general, this is a paragraph. This is a button that links to your site. And then we have another paragraph here. Okay, so here's what we're going to do. In my .env file, why don't we use Mailtrap here?
Testing Email with Mailtrap4:41
Okay, so here's what we're going to do. In my .env file, why don't we use MailTrap here? So let me log in. You can set up a free account. Just sign in with GitHub. And you can see I have a demo inbox here, but, you know, we can create it from scratch. And then if we click through, grab your username, paste it in, and then grab your password and paste that in. So yeah, MailTrap, think of that as just a way to actually fire off an email in local development and see how it would look in real life.
So yeah, MailTrap, think of that as just a way to actually fire off an email in local development and see how it would look in real life. All right, so our routes/web.php file, when we hit the home route, we will notify this User that this lesson was published. Let's come back here. I'm going to give this a load. That's done. We didn't give it any feedback, but sure enough, we can see the email here. And now take a look at this. Kind of cool.
When you call the action method, you give it the button text and the button link. So now if I click on this, it goes to Laravel.com. Finally, this other stuff, this will pull from your application's config. So in my case, notice LaraCasts, and then at the top, well, I didn't show you this, but if you go to config/app.php, right at the top, here's where you can define the name. So we could say Laravel, bring it back to that, fire off the email, and now the heading will be updated accordingly. Laravel, and then regards Laravel. So now how does this all work?
Laravel, and then regards Laravel. So now how does this all work? Well, yeah, if we go to notification/email, this is the actual email template, so to speak, that's being used behind the scenes. So here's what I want you to see here if we scroll past the CSS, quite a bit of CSS. Okay, so we have the logo, and it's going to fetch the app name configuration item, all right? Next, if we come back, here's the body, and we are checking the level.
and it's going to fetch the app name configuration item, all right? Next, if we come back, here's the body, and we are checking the level. So that determines if we're saying hello or whoops. Now, we can set this, if we come back, using either the level method, or you can do things like error and success. And really, all that's going to do is change a heading and the button color. So let's come back and send this again. Now we have a new one. Because it's success, we get green, and we also get this heading. However, if we were to change that to error, well, once again,
it's just a simple Message object that you are populating, where you can set a subject, you can set the lines in it. Here are the setters that we called earlier, or level. So if I want to set the subject, I could just say, the subject is, a new lesson was published. Fire that off, and now you can see that has been updated. What else do we got here? Yeah, set a paragraph, width is kind of a behind the scenes method. We can set the action to set a button. Yeah, so it's actually very, very simple.
We can set the action to set a button. Yeah, so it's actually very, very simple. So that means, in our case, what we could do is something like this. Return a new MailMessage, where the subject is, a new lesson was published. Or how about this? New lesson, and then concatenate this lessonTitle. And then we could say, a new lesson was published at Larrakass. And then let's reference, once again, this lessonTitle. We're gonna give them a call to action, so watch it now. And that will link to, I don't have a path.
Publishing and Customizing Templates9:10
A new lesson was published, the title of it is My Lesson. And we can click through, and that will take us to this URL. Now, it's very possible that you're happy to stick with this template. But of course, remember, you can publish this. So you could say, vendor:publish. So in this case, it's publishing any pagination views, if you wanna change that, or notifications. Or once again, you can always just publish the notification views alone. So you'll find that now within, let's close this up, within your resources/views/vendor directory.
So you'll find that now within, let's close this up, within your resources, views, vendor directory. And now here is that email view that we were just modifying. So for example, let's go to hello. Maybe we want the default for that to be what's up, and an error to be oh crap. Okay, so now we're gonna do a success notification, hit that. And then real quick, let's set up an error notification, which would make sense in this context, but we can see both. Okay, so now, yeah, we've modified this template. Of course, you can change it however you want, style it however you need to.
