This video demonstrates how to design a database schema for storing user notification preferences using a pivot table approach in Laravel. The solution involves creating a notification_user pivot table that links users to notification types, with a JSON column storing the channels (such as email and Slack) for each notification. The video shows how to implement this using a custom pivot model to cast the JSON column to an array, ensuring the data can be easily accessed and used when sending notifications to users.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
User Notifications Preferences SetupAdded:
In the next episode, we're going to look at how we can take all of these checked pieces of information, 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. 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. 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.
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_id.
Of course, it needs to belong to a user as well, or relate to a user. 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 found from experience this is the easiest way, is just have a JSON column with the channels that we want 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. 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. 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, you 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, 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 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. 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. And this uses the has database notifications trait, which sure enough contains a relationship called notifications. So, what we don't want to do is override that. We'll look at that a little bit more later, 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.
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 the channels. These two relate to the actual relationship, but this is just a pivot, uh an additional pivot column. So, we need to include that as well. I'm also going to say with timestamps, 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. We've got this set as a JSON column. We want to make sure the 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 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.
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 out a specific pivot model within Laravel and cast it within that.
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, 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. 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. So, we can say notification and user class. You can 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 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, 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 within array, 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, and then in the next episode, we're going to start to write some tests, so we'll able to verify that this works anyway.
So, notification ID, let's choose one.
So, let's say project created, that is five. 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. 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. 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, 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. 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.
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, 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, 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. 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 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 and we can pass it directly through to each found notification classes when we get to that. And that will tell us what we need to send it out as, 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. Now, we can start to look actually submitting this data through in our notification form and getting this synced up in the database.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsโข2026-05-28
How agent o11y differs from traditional o11y โ Phil Hetzel, Braintrust
aiDotEngineer
450 viewsโข2026-05-28
Re: ๐ฃ๏ธ๐theprophedu๐2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 viewsโข2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation๐ฏโ
LearnwithSahera
1K viewsโข2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsโข2026-05-29
Search Algorithms Explained in 60 Seconds! ๐ค๐จ
samarthtuliofficial
218 viewsโข2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsโข2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 viewsโข2026-05-29











