Laravel Database Notifications

Bojitha Piyathilake
3 min readMar 19, 2021

--

In this article I will lay out the steps on how to create a database entry for a notification and also how to display that notification for a particular user.

The Setup

Before we get started notifying everyone, we need to create a table in the database to store our notifications. This can be done simply by running the following in your terminal;

php artisan notifications:tablephp artisan migrate

This will create a table in your database where you can store those notifications.

Next we have to create a notification model which can be used to create notifications. To do that run;

php artisan make:notification #NAME#

in your terminal.

This would create a new folder in your app folder called notifications and inside it you will find the notification with the name you have given.

Configuring Your Notification

Your newly created notification would look something like;

In the construct function we want to add the data model of the data we want to pass and assign it to a new model available inside the notification class. This isn't compulsory but just makes it easier to manipulate data.

In the via method’s array we include the types of notifications we want to send. By default, it will have the mail notification, we want to add ‘database’.

Then next function is the toMail function. Which constructs the message which will be sent as an email notification. Since I will be demonstrating database notifications, this function can be deleted. For every type of notification we mention in the via method’s array, we need to create a to#### function. For example for the database notification we will create a function called toDatabase. If we were using broadcast we would create a function called toBroadcast and so on.

Otherwise, those notification types will be caught in the toArray function.

To configure it to how we want, lets change it as;

The data item for this notification will be of the process_item model. SO i will accept it and assign it to the local variable called $processItem.

By doing this, it allows us to retrieve the data kept as foreign key relationships.

In the toDatabase function, I’m specifying that the ID, type and the ID of the user who creates the request should be stored as notification data. Similarly you can store any data that that is available from the data which you pass.

Where Do we Call the Notification?

We can call the notification in the controller and it would look something like;

auth()->user()->notify(new StaffAssigned($process));

Take into consideration that process is a data item of the Process_Item model which I have created and saved to the database. I’m using that data to create the notification.

But what if you wanted to send the notification to more than 1 user?

$users = User::where('role_id', '<', 3)->get();Notification::send($users, new StaffAssigned($process));

This gets all users whose role_id is < 3 and creates notifications for all those users. However in the notification table in the database it will only create one entry because it is a polymorphic m-m relationship which exists between users and notifications.

Displaying Notifications

In any blade file we can use;

@foreach(auth()->user()->notifications as $notification)ID:{{$notification->data['id']}} - Type:{{$notification->data['type']}}@endforeach

To display the notifications available for that specific user.

But what if we want to only display the unread notifications?

Marking All as Read

If we want to mark notifications as read, we simple need a link and a route.

Create a link or button to click to mark all notifications as read;

<a href="/markAsRead" style="color:green"> Mark All As Read</a>

Then create that route in the router;

Route::get('/markAsRead', function(){auth()->user()->unreadNotifications->markAsRead();return redirect()->back();});

This will mark all notifications as read and redirect back to the same page.

Now how do we get only the unread notifications?

@foreach(auth()->user()->unreadNotifications as $notification)ID:{{$notification->data['id']}} - Type:{{$notification->data['type']}}@endforeach

Instead of calling for all the notifications of the user, now call only the unread notifications.

Simple, but very useful. Hope this helps.

--

--

Bojitha Piyathilake
Bojitha Piyathilake

Written by Bojitha Piyathilake

I am an undergraduate at the University of Moratuwa following a degree in Information Technology and Management.

Responses (1)