This episode is for members only

Sign up to access "Building a Notification Preference System in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
03. Notification groups, types and channels

Transcript

00:00
By the end of this episode we'll be able to list out all of the groups that our notifications
00:04
should belong to, each notification underneath that and then all of the channels. Now these are all going to be driven from the database and of course this is completely optional, but when I built this system I was building it for myself and I really needed an easy way to manage all of the groups, the notifications and the channels without having to dive into code.
00:28
So although we're going to be going a little bit over the top with storing everything here in the database, of course you can go ahead and change this up later or now if you need to. Okay let's get started on the overall thing that we need here which is the groups for the notifications. So let's go ahead and make out a model here and let's
00:47
call this notification group. We'll create a migration and a factory alongside of this so we can seed this data when we write our tests. Okay so let's go over to create notification groups table and let's fill this in and we pretty much, at least just for now, just need a title here and of course you can add more. So let's go ahead and run phpartisan migrate
01:12
and there we go. So the next thing is going to be the notification itself. So let's go ahead and make out a model called notification and it's just going to be notification again with the migration and a factory. This is going to be slightly more complex, it's not going to be massively complicated. We need a title and a sort of description of this or a identifier and a title,
01:34
whichever way around that you want to do this. So in our case we are going to, well first of all, associate this with the notification group id. So the notification group is the table that we've just created an id will be the foreign key and then we'll have a string in here for the type of this. So we'll have a sort of unique key for the type and that's going to be really useful
02:00
a little bit later when we actually push this into our notifications or map this up with our notification types. So for example if we had a project underscore created notification type and we gave that a name within the actual notification class itself, we have a way to map that up to the database then and find out the preferences for that specific user for a specific
02:22
type. Basically it's just a unique key and we'll see that a little bit later. Okay now that we've got this let's go ahead and run our migrations on this as well and then lastly just before we start to map everything up here we'll go ahead and create out our notification channels model. So let's go and create this out as well make model notification channel and again a migration and a
02:45
factory alongside of that as well. Let's go ahead and open up the notification channels table migration and again pretty straightforward here we just want a string in here for the title so that's going to be something like sms email slack whatever notifications you're allowing and then we're going to have just a string in here with the type which is just going to be a unique identifier
03:09
for each of these so again that's going to be something like sms email slack but probably lowercase. Okay let's go ahead and run our migrations on this and we should be good to go. Okay so we're going to dive straight over to the notification group because in this model we know that we are going to have a bunch of notifications underneath each of these groups so let's go ahead
03:33
and return and say this has many and we'll say notification and class. Now we're not going to seed this data just yet I'm going to leave that to the next episode because we're going to set up a seeder that we can use in production once we launch our app so we can either create for the first time or add to all the notifications that we have. Let's create these out in a database first of all
03:57
we'll start to iterate through them and we'll see what it looks like and then we can build out our seeder. So for notification channels this is pretty straightforward let's go and include two so let's say email and again for the type I'm just going to set that to a sort of lowercase string version of this let's open this up so we can see and let's have slack in here as well
04:21
and let's just modify the date that this was created just for the sake of it and there we go so we've now got our two notification channels now that's really straightforward because we can just start to iterate these directly over here to create out the channels and we can do that inside of here as well for each of the check boxes so that's a pretty straightforward one. For the groups
04:42
let's just create out a group one just to keep things really simple here and we'll go ahead and just open up these column widths and let's say group two and then let's just assign a couple of notification types under each of these so for one let's say project uh let's say notification one we'll get to the point where we make these a little bit better a little bit later so that's
05:13
notification one save that out and open this up let's duplicate this down to notification two the same there and then for the second group let's bump this to three and then for the second group as well let's bump this to four so we've basically got two under each of these sections now the whole point of this is just to have enough data to iterate this and get this out on the template
05:39
so let's go ahead and start to do that now okay so we know up here we've got all of our notification channels which live in this column so we need to pass this data down of course that needs to come from our notification controller so we can just pass this directly down from here so for the notification groups let's grab the notification group here and let's just say get now of course
06:05
later on you can order these you could add an order column if you needed them in a specific order you could order them by title whatever you wanted to do we also are going to have all of the notification channels in here so notification channel which is what we'll probably start with i'm going to put them at the top as well so for the notification channels we can just start to
06:29
iterate through these here so let's do that now for each notification channels as notification channel let's grab get rid of these comments grab all of this put that directly inside of there and let's output the notification channel and title we should now have email and slack great so now what we can do going into this matrix we can now iterate over each of the notification
06:55
groups so let's do that in here so for each and let's pop that down there and for each let's say for each notification groups as notification group or group and let's open this up this is the group title so notification group title let's just have a peek at that and there we go so we've already got this sort of looking a little bit better with each of the groups that we've created in the database
07:26
and then of course each of the notifications that we've stored in the database come underneath the group itself and we've created the relationship for that so all we now need to do is just start to iterate through each of the notifications so let's say for each and let's grab this code pop that in there and let's say for each notifications group notifications so we're now working with a
07:48
relationship as notification then in here we can now output the notification title so we're basically just going through in layers and outputting everything that we need and you can see here now we've got the first two notifications under group one and under group two as well now we need to figure out these two things which is basically just iterating over the notification types again
08:15
so we're just going to do exactly what we did at the very top here so for each notification channels as notification channel then we want to do this which is outputting a check box for each one and that should be everything that we need to do so what this now means is however many channels that we add in the database or within our admin panel or however many notifications or groups
08:42
that we add and assign these notifications to this will just build up this matrix for us so we can just start to select each one now we still need to work out how to name these how to pass them through to our controller to actually sync them to the database but for now we've got a really nice flexible way of outputting each of these groups each of the notification types and each of the channels
10 episodes1 hr 23 mins

Overview

Need to store advanced notification preferences in your application?

In this course, we cover displaying a matrix of notification groups, types and channels with the ability to sync preferences cleanly and quickly.

Once you’re done, your users will be able to choose what notifications they receive, and how.

Oh, and we’ll write tests to back everything up, look at how to use notification preferences in Notification classes, and how to set defaults for when users register.

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

Episode discussion

No comments, yet. Be the first!