Introducing Notifications0:06
Let's move on to notifications. A notification allows us to notify a user, as you would've guessed. So to illustrate this, we're gonna keep it very basic. Why don't we say, whenever we persist a new idea in the database, as part of that, we're also going to notify the owner. Let's get going. Here is my new idea. If I were to submit this form, and I'll hold off on that for just a moment,
Tracing Store Action0:27
If I were to submit this form, and I'll hold off on that for just a moment, well, where does it go? Well, you'll remember that it makes a post request to the ideas endpoint. You can go to your routes file and you can see, all right, when we make a post request to ideas, we hit the store action on Idea Controller. So if we visit the store action, it simply whips up, uh, a new idea in the database, it associates it
Exploring Notifiable Trait0:45
So if we visit the store action, it simply whips up, uh, a new idea in the database, it associates it with the current user and then it redirects back. Very simple. But now here's what we're gonna do. We should also notify the user. Okay, so how can we do this? We do this via notifications. So take a look at this. If I go into my user model, you'll see that by default it uses this trait called notifiable. And if I click through here,
called notifiable. And if I click through here, we can use database notifications and routes notifications. Let's have a look at this. And yeah, it looks like we have methods for notifications and notifications. You have read and notifications that you have not read. Alright, so we have access to these methods off of user. Interesting. So why don't we test this out from the CLI. Let's run Tinker. Let's find a user.
Interesting. So why don't we test this out from the CLI. Let's run Tinker. Let's find a user. So we'll just find whoever the first one is. All right, John Doe. And now let's say, all right, give me the notifications. So again, if I switch back, you'll see these are methods we can call that reference relationships. So I can call it as a property, but it fails no such table notifications.
Migrating Notifications Table1:55
So I can call it as a property, but it fails no such table notifications. Okay, so it looks like in order to make use of this feature, we need to run a migration to set up the notifications table. Let's see if we can generate that PHP Artisan. And yeah, we're just gonna look for notifications and sure enough, we can see a command that will build that up for us. Alright, let's run it PHB Artisan,
that will build that up for us. Alright, let's run it PHB Artisan, make the notifications table and then I will migrate my database. Now we have a new notifications table, so let's try it again. I boot up Tinker, I try to access notifications for this first user, and we get a collection of notifications that, of course is empty. Okay, interesting. So let's do this.
of notifications that, of course is empty. Okay, interesting. So let's do this. Let's say this to John and I wanna notify John that something happens. Well, what can we do here? Is there a notify method? Well, let's switch back. Let's go into routes notifications. And yes, there is, there's a method called Notify. And there's also one called Notify Now, which is the same thing, but it will be synchronous, uh, rather than, uh, as part
Creating Email Notification3:00
which is the same thing, but it will be synchronous, uh, rather than, uh, as part of a chew that runs in the background, which you will often wanna do for emails, by the way. So it looks like we can call a notify method on our user object, and then we just need to pass it whatever notification we wanna send. Alright, that's our next step. PHP Artisan. Let's make a new notification. All right, so what do we wanna call it?
Let's make a new notification. All right, so what do we wanna call it? Invoice paid distinct sample here. How about, um, IDEA published? All right, idea published. Do we wanna mark down view? No. All right, now we have a new, uh, notifications directory that you'll find within the app folder. App notifications idea published. Okay, so here in the Via method we can specify how we want this notification to be dispatched.
Okay, so here in the Via method we can specify how we want this notification to be dispatched. Basically, do you wanna send an email? Do you want it to be a database notification so that the user sees like a little, a little alert, uh, next to their avatar and they can review all of their site notifications. Do you want a text message? Uh, you could do one or all three of those at the same time. Alright, so for now though,
Uh, you could do one or all three of those at the same time. Alright, so for now though, we're just gonna stick with an email. And here's how we can define the shape of the email. So notice we can use this simple fluent API to structure it. So for example, I can set a greeting and say, hello, you published, uh, and this is transactional. You published a new idea, read it. And this will be a URL. And let's just extract this.
You published a new idea, read it. And this will be a URL. And let's just extract this. The URL is gonna be a, um, a full uur L to ideas slash and then we need to accept the idea. Uh, id. All right, thanks for using our app. Something like that, you get the idea. Alright, so let's format this and then make sure that within the constructor we accept an idea.
and then make sure that within the constructor we accept an idea. So I will accept that and then we will, uh, directly assign it. Now, if we want this to be queued, we can implement the should queue interface, but we haven't yet talked about queues. Uh, for now, just think of queues as a way to do something, uh, behind the scenes without making the user wait. As you can imagine, sending emails is actually still a
uh, behind the scenes without making the user wait. As you can imagine, sending emails is actually still a lengthy process and we don't actually need the user to wait for it. We can do it behind the scenes as part of a queued job. Okay, but let's put a pin in that for now. So this is good. I have a, an idea published notification. It expects the idea that was published as its constructor arg, and then it will build up a simple email
as its constructor arg, and then it will build up a simple email and deliver, um, deliver it to the user. So let's give it a shot. Once again, PHB artisan tinker. I will t track down John and I'll say, John, I want to notify him that there was a new app notifications idea published. And then let's make sure we sent through the idea, uh, that was published.
And then let's make sure we sent through the idea, uh, that was published. So I'm just gonna once again grab this on the fly. I'll say, give me the most recent published idea and we will just assume that was the one he created. So if we give this a run, we get null, but sure enough, an email was delivered. Okay? So now we're in a situation where it would be helpful to see one, what that email looks like, and two, preview what this notification will look like
to see one, what that email looks like, and two, preview what this notification will look like before it actually gets delivered. So lemme show you both first back in my editor, if we open up our environment file, if we scroll down to the mail section, you'll see that by default any email is going to be logged and uh, yeah, it's a sensible default for local development. In real life, you might use SMTP, you might use something like postmark,
In real life, you might use SMTP, you might use something like postmark, um, whatever you want. Okay, so that means if I check my Laravel Logs file, I should probably see this email. All right, let's do it Now. Logs are stored within the storage directory and you'll find that right here, storage logs. And here's our Laravel log file and sure enough, here we go. So we can see the full body of the email including all
And here's our Laravel log file and sure enough, here we go. So we can see the full body of the email including all of the, uh, the meta information. But if I scroll down, sure enough, I can see, hello, you published a new idea, read it. Here's a link to it. Thank you for using our app. This is all looking really good. But yes, now of course I would like to see it as part of an actual male gui. Okay. Alright.
Previewing Emails Locally7:29
of an actual male gui. Okay. Alright. So as it turns out, there's actually a handful of tools that we can reach for that are really great for testing your emails. Things like Hilo or Mail Trap or Mail Pit, which I'm using here. Uh, you can just try 'em out. Pick one that looks good to you. So let's do mail pits. And I've never even used this before
Pick one that looks good to you. So let's do mail pits. And I've never even used this before and I'm just gonna show you how easy it is to set up on the fly. Let's go to installation. And it looks like we can pull it in on the Mac using Home Brew Fine. I will paste that command in. All right, and once that's done, we can run Mail Pit to boot it up and yep, that's accessible here.
All right, and once that's done, we can run Mail Pit to boot it up and yep, that's accessible here. All right, and we have our gui Perfect. So now we just have to set up our port, which is 10 25. I just happen to know that. And the next time we deliver an email, it's gonna show up right here. Alright, to our editor, let's open up our environment file our mail mailer is now going to be SMTP. The port is 10 25 on local host. And the from Global address will be, you know,
The port is 10 25 on local host. And the from Global address will be, you know, admin@larrickhouse.com, whatever your app happens to be. Okay, so this looks good. Let's give it a shot. Let's find John, let's notify him. And now if we switch back, ta-da, it works. We have a new incoming email. You publish a new idea with no punctuation, shame on you, Jeff. And if we take a look, we click on it
Wiring Notification in Controller8:50
with no punctuation, shame on you, Jeff. And if we take a look, we click on it and sure enough we can see it looks like John wants to learn about being a farmer. It's working. Okay, so now the only remaining step is just to make it not something we do from the command line, but instead something we do as part of the controller action. So let's open up our idea controller and within the store action,
So let's open up our idea controller and within the store action, after we have persisted it, let's notify the user. And I'm just gonna paste in what we had before, but of course we're not gonna hard code John. Instead we're gonna grab the authenticated user and then we'll say, notify them that a new idea was published. So let's import that. And now rather than manually fetching a random idea, we will use the one that was just created.
So let's import that. And now rather than manually fetching a random idea, we will use the one that was just created. Okay? So as part of storing, we will persist a new idea for the user. We will notify them and then we will finish by redirecting back to their ideas list. Let, let's give it a shot. Let's create a new idea. I want to learn more about Laravel notifications. We create it all right, there we go.
I want to learn more about Laravel notifications. We create it all right, there we go. And we should also have a notification, local host 80 25. Here it is. We have an email. You publish a new idea. We should probably even include the title of that. And this is great. You would just do something like this idea published. You would update this line would be this idea, uh, what do we have?
You would update this line would be this idea, uh, what do we have? Is it description? Uh, now we have it, then we have a link to read it. And you get the idea, read it, and there you go. It's working. All right, so now you know what, this is gonna take you a really long way. Uh, most of the time you just wanna send your notifications via email. But if you also want a notifications widget on your site,
notifications via email. But if you also want a notifications widget on your site, you can use the database driver if you want to use a a text message. Then there's an SMS driver. And then as actually as it turns out, there's dozens of community provided drivers as well. So it's really quite extensive.
