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
04. Seeding and listing all available notification types

Transcript

00:00
If you're integrating this into your own applications, chances are you're going to need
00:03
to seed an initial batch of notification data and then of course through your admin panel, whether you're using Nova or Filament, you can go ahead and continue to add notifications. Let's work on this so we have an initial batch of groups, notifications and channels that we can use. We probably won't do this for channels but at least for the groups and notifications,
00:23
so when we launch this feature we're ready to go and we can just start to do this. Okay so to get started I'm going to get rid of all of the notifications that we've just created. I'm going to get both of the groups that we've just created just so we end up with nothing in here and let's go ahead and create our a seeder to deal with this. So let's go ahead and say
00:43
make seeder and we'll call this notification seeder. We'll just keep this really simple and of course you can create one for notification channels as well. So our notification seeder is going to be really careful in the fact that it's going to create out a group only if it exists. If it doesn't it's just going to grab the first instance of it and then through each of these
01:05
top level groups we're going to create a notification again only if it doesn't exist. So if we run this seeder twice it's not going to double up on the notification types or groups that we create. So let me show you an example of this and then I'm going to go ahead and fill the rest in and of course you can grab this code from the github repository if you want to. So we're going
01:28
to go and create our notification group but we're going to use first or create here which will not create this if it exists based on the attributes that you've passed in. In our case it's going to be the title so we're going to call this first group projects. Now underneath here what we can do using that relationship that we already used to output these notification types underneath
01:52
is do the same thing. We can say first or create and again giving the attributes that we want to create this for uniquely. So I'm going to say project underscore created and the title for this is going to be a new project is created. That's for the user's benefit. So we can do exactly the same thing for the second group as well. So let's create another group in here and imagine that our
02:15
application deals with some sort of files and we want some notifications to do with files. In this case let's go ahead and give the overall group a name of files and then let's say file underscore created and we'll say when a new file is created we're going to get notified with based on any of the channels that we have chosen. I'm actually going to call that uploaded. So now with this
02:39
seeder we can add as many notification types underneath each of the headers we want. We can run this as many times as we want without doubling up on the data. So when we run phparts and db seed and then we choose that notification seeder specifically that goes ahead and seeds our database which is again great for production. We can just run this once we've launched our app
02:59
but if we keep running this it's not going to go ahead and interfere and double up on this data. It's not even going to change anything about this it's just going to do nothing. Okay so what I'm going to do is go ahead and just add in a few more notification types here just so we have a good amount of data that we can use to test this and I'll be back with you in just a second
03:22
and of course you can go ahead and copy and paste these over from the github repository if you want to. Okay so I've gone ahead and added in a few more notification types here again I can just go ahead and rerun notification seeder and that's not going to recreate the ones that we already have in the database. That's given us quite a few to work with now and now we can sort of
03:42
select the ones that we want like this go ahead and submit it through and of course make sure everything gets updated. So there is our nice seeder that we can use to work with and we can use that in production and then just add on any other notification types we need in future via an admin panel.
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!