Laravel 8 with notification management
Tags: laravel, notification, php, tutorial
Published: 2021-11-14 03:37:02

We talking about how to create the Laravel 8 with notification management. We early created multiple things of Laravel. Notification is one of the valuable parts of the web application. why for I said that one? Sometimes we are allow to access users for payment or any financial thing. We want to record everything for that. We will create transactions or something. But notification is very readable for than notification. Let's see how to create that one.

I used the early created project for this one. [Click Here](https://dev.to/jsandaruwan/laravel-8-with-bootstrap-58bm) 

Go to the project path and open the terminal. 

```
cd bootstrap-app
```
First of all we want to create notification table for that part. After create that table we will migrate again our database. 

```
php artisan notifications:table

php artisan migrate
```

After that type the code for create notification. You can any name for that notification.

```
php artisan make:notification PaymentsSave
```

For this I used the User model therefore we want to add the use Notifiable for our user model like that way.

```
<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;
}
```

Awesome now we looking for how to use that notification use our system. 

```
$data['message']="The Payment Successfully !"; 
$user->notify(new PaymentsSave($data));
```

That way we can use the notification with parameters. Let's see how to fix PaymentsSave class. Go to the PaymentsSave class you can see multiple function on it. Go to the **via** function we can config that use sent class use for sent email or save data in database. That time we are use save data in database. Therefore **via** like this.

```
public function via($notifiable) 
{ 
    return ['database']; 
}
    
```
we want to edit the **__construct** function also that way.
```
public function __construct($notification)
{
   $this->notification = $notification;
}
```
After we can edit the  **toDatabase** function like that 

```
public function toDatabase($notifiable) 
{ 
    return [ 'message' => $this->notification['message'] ?? '']; 
}
    
```

Remember that  I will sent the data with message that will save the our database as JSON array in notification table data column.

Now, how to show the notifications?

Let's see this. We want to object for user model for like that **$user**

```
$user->notifications;
```

###Get Unread Notifications
```
$user->unreadNotifications;
```

###Get Read Notifications
```
$user->readNotifications;
```

###How to update status Read Notification
This one we want to pass the parameter of notification ID
```
$user->notifications->where('id', $id)->markAsRead();
```

###How to update status Unread Notification
```
$user->notifications->where('id', $id)->markAsUnread();
```

###How to update status All Read Notification
```
$user->notifications->markAsRead();
```

###How to update status All Unread Notification
```
$user->notifications->markAsUnread();
```

That way to create notification management.Let's meet again for the brand new tutorial. 

Thank you,
J-Sandaruwan.