Laravel 6 Advanced - e10 - Notifications

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] as the next topic of conversation I want to bring in notifications notifications are a robust feature in laravel that allows us to notify users but it goes above and beyond that during this episode let's go ahead and start to explore everything that notification has to offer for us first thing to understand about notifications is that they are driver driven and what that means is that there are many different drivers for this particular feature you don't have to stick to just one thing and as a quick side note on that there are first party drivers for example for even text messaging notifications as well as slack notifications now these do come as a separate package that you have to bring into your project but it is very nice to have some first party code that you can bring into your application to add even more functionality into this notifications feature so let's just start right through it and let's start to explore what level actually comes with if we go into phpstorm this is a completely fresh laravel project the only thing I've done is to set up my environment with a MySQL database if you go into app it obviously ships with your user and every user model has this trait of notifiable okay let's go into notifiable so notifiable is a trait and it actually only has two traits inside of it the first one is the has database notifications and the second one is the routes notifications let's take a look at this routes notification if we go in here we see that there is this notify method that we could call so through the magic of object-oriented programming at the end of the day you'll be able to do something like grab a user and hit the notify and then do some sort of new invoice paid or something like that so that's where that method comes from now all it does is it goes ahead and grab your dispatcher and it sends through this particular notification now you also have notified now which is just another version of it which is immediately because again this could be queued and this could be now a little bit of a slightly different thing a little bit beyond the scope of this but take you to a lesson then you have route notification for where it gives you the driver not caring about anything else and that's it so it's a fairly simple class here let me show you how to use this notify as you may know you do have a factory for a user let me jump into the terminal and the first thing I'll do is migrate the database PHP migrate okay now let's take a look at sequel Pro here so it was empty before and now we've got just your typical level stuff here's my user perfect let's go ahead and create one PHP artisan tinker let's go ahead and create some users so we could use the factory app user class let's go ahead and create one so now we do have one user awesome now let's go ahead and also setup male trap I do want to do some testing so here's my male trap account if you don't have male trap it's a free service that you can use for testing emails now don't copy my user name and password because you won't see it in your own mail trap go ahead and get your own account they are free all right let me jump back into phpstorm EMV let me also set up in my mail section here my username as well as my password alright there we go now that we have that one user let's go ahead and create a new notification through the documentation it talks about this idea of an invoice getting paid let's just stick with that to keep it simple PHP artisan help make notification okay so we see that we can create a fresh notification class right off the bat and it doesn't really need anything but a name okay HP artisan make notification invoice paid so now in phpstorm let me close this up we now have a new section here for notifications that invoice paid and now let's explore this class this is a very important class and it's actually very powerful the first thing you'll see here is well you have a construct method right and that is so that you can accept any data that you need that you needed the actual invoice maybe that's one of your models so we could say something like invoice invoice for example we won't we don't really have that so we'll leave it as this then you have this via method in this array you could specify any number of channels that you would like the user to be notified by at the end of this episode we'll talk about how to customize this and I think you'll really like that approach as well but for now we see that we have mail meaning email right available now for every one of these you're going to have a matching public function and it needs to be a particular naming convention and it is you grab this name and you put two in front of it the male becomes two male if you had for example a slack notification up here like so then you would have to have another public function here called two slack and that's just the way it works whenever it gets to this it will actually try to call the method with the matching name that's pretty cool because it allows you to customize each channel but you only send one notification then the inside of it actually takes care of that furthermore this right here actually allows you to modify that for each user and we'll take a look at that at the end of the episode let me go ahead and get rid of that and let's just stick to emailing for now so right off the bat it comes with a nice message that we can already start testing with the last method here is to array as you can imagine there is always going to be some sort of default and this is it the two array is the default notification okay so now that we have this let's give this a shot let's head back into tinker PHP artisan and grab that user again so user first okay and then I could say user I want to notify you with a new app notifications invoice paid we don't have any arguments for that and we'll send that through okay let's see if it worked there it is but we have our invoice paid coming through mail trap so there we go so this is pretty cool this is again a nice way to be able to do this in one single place now what about database so database notifications are something you may use if your application has something like a social network little bell where you could see what notifications you have pending and the nice thing with it is that comes basically ready to go the only thing you'll need to do is run this command here so that it adds the migration that you need you don't even have to design it so let me go back in here let me exit out of tinker let me paste that one in and now we'll need to do PHP artisan migrate okay so now we have a new notifications table let's take a look at that and there it is so my notifications table has a type notifiable type a notifiable ID because it is a many-to-many polymorphic relationship then we'll have this data key which we can pass through I'll show you how to put some data in there and then we have this red @ it's basically a timestamp where you'll know if the user has read the notification or not now it doesn't work automatically but I'll show you how to make it work so the next thing I'd want to show you is back in phpstorm let's head back to the user model back into this notifiable trait and there's this other trait past database notifications when you have this it actually has that relationship that I was talking about but you'll have this new notifications relationship and again it's a morph many-to-many and then you also have the red notifications as well as the unread notifications that's pretty cool because it allows you to retrieve only red ones or only unread ones or all of them simply with notifications okay but how would we use this well first of all we need to go back to invoice paid and we need to add this to the stack think of this as just a stack of different things that you want to notify the user with is ad database to it and of course let's add another public function here for to database and then in here you're going to return an array and the array is going to contain data whatever you returned in this array will get plucked into this data column right here so let's just make up some data what about amount we'll say that this invoice is worth a thousand dollars maybe we'll have an invoice action which we will just say pay now I don't know I'm just making this up it really doesn't matter what this is I just want to show you how to add some data okay so again we added database to our via method and then we've also got this special method here which is named just after the same exact thing the database becomes two database you'll do a capital letter on the first word okay let me head back in to tinker again let's try this one more time I'll grab my first user then I'll notify that user again now this time yep it's inside mail trap again but now we've got this new notifications table and that's pretty cool notice how everything is filled out for you but we'll have our data here notice my amount as well as my invoice action of pay now so any data you need to store that it's only particular to this notification would go there but then the read at is not set and that's okay I'll show you that in just a minute so now with my user I can say give me your notifications and there it is that's that same exact notification notice this is just a collection of notifications it's actually a collection of database notifications read at is not set you can also call unread notifications gives you the same thing in this particular case as well as red notifications which will be empty okay so how do we mark one of these as red well somewhere in your UI the user may have a little X or something like that where they get to click that and mark this notification as red when they do you're gonna grab that notification so in our case let's go ahead and grab the first one say notifications don't grab the first one right this will give me a database notification instance so I could do on that I can do mark as read and now if we fetch all of our notifications notice that reddit is now set and of course in our database it's been read at so that's pretty much how notifications with database works the pretty cool stuff but there's a lot more to notifications and like I said before what if you wanted to customize this for each user well here's an idea let's go into the create users table this is the migration that actually comes with laravel let's add a new string here for we'll call it notification preference maybe this will make it a default we'll give it a default of mail for example okay gonna have to go ahead and dump the entire database at this point let me exit out a tinkerer PHP artisan migrate fresh whoops brush there we go okay so everything's gone now but our users now have this notification preference okay let's start to work with this let's go back in here and let's make a new user again PHP artisan tinker will use the factory and will create one and of course that'll have a default of mail so how could we fetch that well as it turns out whenever you are inside one of these notifications you receive this notifiable and that is actually a user instance check this out let's die and dump notifiable let me go ahead and try to notify this user I have to exit out a tinkerer and start over because I did change my code so we'll grab the first user and then we'll go ahead and try to notify this same user and now it dies and dump and what it's giving us is a user instance okay right here so we do have access to that one notification preference that I was talking about okay so instead of having an array why don't we instead do a comma separated list what we could do is grab notifiable notification preference and I can explode into an array just using maybe a comma-separated list we could say explode the delimiter is going to be a comma on a space and then the subject is going to be this right here so instead we're actually going to return this we'll return it right here so that basically doesn't change anything all it's doing is just grabbing from our database and notifying the right user okay let's make sure that that works though let's hop back in to tinker let me go ahead and grab my user again and I'll notify them and it looks like it went through I should not have a database notification you see there isn't one but I should have one here male trap and sure enough I do okay so now imagine that that user then said well I also want to be notified via database as well so now that user is going to receive mail and database all right let's give that a go again let me go ahead and exit out of tinker back up grab my user and then notify that user now in my notifications I do have that as well as in mail trap so how cool is that now we're able to notify them but at their preference we're actually asking them what they would want furthermore this is great for things like text messaging or slack notifications and that's awesome because black is great even for just internal use when you want to know what is happening in your application so let's talk a little bit about other things that you have let's do a quick search here for slack and there we go so one of the prerequisites for slack like I mentioned at the beginning of the episode is that you have to bring in this first party package which is okay is not a big deal it's made by the creators of level and they allow you to hook up to different notification channels so in this particular case this is the slack channel and they also have one with next mode where you can send text messages but the slack channel is actually really really good check this out you can actually format your notifications to have very cool things even down the tables so you could have tables you could have all sorts of cool stuff and again it's all basically built into level that you're able to do that so where to go next with notifications well notifications should replace anywhere in your application where you're finding yourself having to send maybe two or three different things to users when an action is taken whenever that happens that is your cue that you should be using a notification instead or of course if the user wanted to customize how they want to receive notifications notifications would be another great thing for that furthermore notifications can be sent without a user so let's explore that and then we'll call it a day we take a look at the on demand notifications notice that here it says sometimes you may need to set a notification to someone who is not stored as a user I can think of this as just as a one-off notification for some odd reason that you need to send so you can actually do that as well and you can still use the notification everything that we learned in this episode with the difference being that this one gets made up on the fly and it is not to one particular user so you could maybe send a notification as an email and then simply notify them of this part take Euler notification so do keep that in mind whenever you're using notifications they don't have to be associated with a user and the very last thing I know I said we're gonna finish it up but don't forget you also have custom notifications you can write your own custom channel you have something particular to your liking that you need to use notifications for you could write your own channel and for example in this case they have a voice notification which doesn't really exist just as an example but it's a great way to know that you could tap into the power of notifications and create your own custom channel so with that we'll wrap it up for notifications I hope this gives you a great overview on how to start using notifications in your application as always thanks for watching don't forget to subscribe I'll see you in the next lesson
Info
Channel: Coder's Tape
Views: 82,662
Rating: undefined out of 5
Keywords: laravel 6, laravel tutorial, laravel best practices, laravel best tutorial, laravel best packages, laravel 6 tutorial, laravel preview, laravel 6 what's new, laravel, laravel service container, laravel ioc container, service container, service container laravel, container laravel, laravel container, laravel ioc service container, laravel advanced, laravel notification, laravel notification tutorial, notification laravel, notification laravel database, notifications laravel
Id: upKOwoe8LsM
Channel Id: undefined
Length: 16min 43sec (1003 seconds)
Published: Sun Nov 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.