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
05. User notification preferences setup

Transcript

00:00
In the next episode we're going to look at how we can take all of these checked pieces of information,
00:06
go ahead and transform them and then sync them with the user. But of course first we need a place to store this and figure out where we're going to store what channels need to be allowed for which notification. So let's go over and start to create the migration out for this and we'll see what this looks like.
00:24
So this is just going to be a standard migration that we make and we're going to create out a notification user table. So this is just a pivot table between the user and each of the individual notifications regardless of the group that we've created.
00:40
Okay so let's go over to the create notification user table migration and let's start to think about what we need to do. So each of these notifications will be inserted into the database or each of the records for this table will be inserted into the database
00:58
and that will hold a list of channels for each notification type. So the first thing that we need to do is relate this back to a notification type so in our case that's just going to be notification under Scott ID. Of course it needs to belong to a user as well or relate to a user
01:15
so we need to know which user this actually relates to. And then what we're going to do and there are a couple of ways that we can do this but I've found from experience this is the easiest way is just have a JSON column with the channels that we want
01:29
to send these notifications or enable these notifications for. So imagine that we had the create project notification let's imagine that that is an ID of one. Our user is an ID of one so that gets inserted.
01:42
All this is going to do is insert a record like this so we'll have email and slack or just slack or just email and of course when we sync that again if we've updated this within the user interface that will go ahead and insert the updated value.
01:58
That's pretty much what we're storing here. Now that makes it incredibly easy for us to go ahead and then grab this out and use it directly in a notification to determine how we're going to send this to the user. What it does complicate is if later on you remove a channel
02:15
you've still got that data hanging around but you could easily write a little script just to tidy up any of the channels that don't exist but of course also you could disable these directly within the notifications. We might look at that later but this is pretty much what we are doing
02:30
and it's the easiest way we don't want to over-engineer this too much. Okay we've migrated our changes let's go over to our user model and let's set up the relationship for this. We're not writing any tests just yet but we will be using all of this functionality
02:46
when we start to write tests in the next episode. So let's go ahead and figure out the name of this relationship. You can call it whatever you want. Now we're not going to call it notifications that's really important.
02:57
I'm going to call it notification preferences instead. Now the reason I'm not going to call it notifications, firstly it's pretty vague but also we have the notifiable trait for Laravel's notification functionality
03:11
and this uses the has database notifications trait which sure enough contains a relationship called notification. So what we don't want to do is override that. We'll look at that a little bit more later
03:25
but let's call this notification preferences for now so it's super clear. Okay so we have a pivot table here so we have a many to many relationship so we're going to use a belongs to many in here to map this up to each of the notifications that we've just created.
03:41
Now that's not just it. So this will create the relationship for us but remember that we've got the pivot column which we have created here which is channels.
03:51
These two relate to the actual relationship but this is just a pivot, an additional pivot column. So we need to include that as well. I'm also going to say with timestamps
04:01
so we store the timestamps when these get synced and we're going to say with pivot and we're going to say channels. Now what we'll also need to do here is we need to make sure that the channels is cast to an array.
04:14
We've got this set as a JSON column. We want to make sure that channels is cast to an array. How do we do that? Well in a model, so just imagine that we had this within our user model
04:26
for whatever reason, how would we do this? Well we would use protected casts then we would choose channels and then we would say something like array or JSON.
04:36
We can't do that because we're working here with a relationship which uses a pivot table. What we can do though is we can create our specific pivot model within Laravel and cast it within that.
04:50
So let's do that now. So we're going to say php artisan make model. We're going to call this notification user so that is the pivot or the kind of name of the table that we've created
05:02
but we're putting this in model form. Then we use the pivot flag and what that will do is create out a model. So let's open that notification user up here.
05:14
That creates this but it doesn't extend model, it extends pivot. Now what we can do is we can say within the relationship where we say with pivot, we can say using and then we can give the model that represents this individual pivot table just here.
05:29
So we can say notification and user class. You could put that in a different directory if you wanted to but now we have a model which represents the actual pivot table itself which means what we can now do
05:40
is we can now cast any of the pivot columns directly in here. There might be a different way to do this but I always like to create out a separate pivot class for this. So we want to cast channels
05:53
and we know that is going to be an array of channels. So when we access these channels these are now actually going to come back as an array rather than a string with an array
06:04
which we would have to manually go ahead and decode using PHP. Okay now that we've got this relationship set up let's just manually create one out in the database try and access the channels just so we know that this is working
06:17
and then in the next episode we're going to start to write some tests so we'll be able to verify that this works anyway. So notification id, let's choose one. So let's say project created that is five
06:28
so let's go and create that under five. My user id should be one. Let's have a look there and yeah it is. So let's go ahead and create that under there
06:36
and I'm going to choose that I want both of the channels here email and slack. So I'm going to say email slack and let's fill in created at just inside of here as well
06:49
and there we go. So that is pretty much eventually what's going to happen when we fill this form out. So if I would have selected both of them and hit save
06:58
that is exactly what would have got stored. Okay so now that we've got this let's just dump this out somewhere within our web root it doesn't really matter.
07:06
Okay so let's go over to our dashboard here and let's die dump off user and notification preferences and let's go back over to our dashboard and see what we get. Okay great so we get a collection of notifications in here
07:23
we've got the actual notification data so we know what it relates to but then we've got the relations in here which is that pivot
07:29
so we can access that pivot information and of course that has the channels in here. So let's just go in here and say first and then let's say pivot
07:40
and then let's say channels so that just grabs the first notification type we have stored which is the only one we have stored here and that grabs the channels
07:47
now that should now because we've cast it come back as an actual array. Remember if we hadn't have done that so let's head over to our notification user
07:56
and just uncomment that that's going to come back as an actual string so really important that we actually cast this properly so we get this back
08:03
and we can pass it directly through to each of our notification classes when we get to that and that will tell us what we need to send it out as
08:11
or what the user has chosen to send it out as. Okay great so we have now successfully set up the relationship between our user and each of the preferences
08:20
now we can start to look actually submitting this data through in our notification form and getting this synced up in the database.
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!