Laravel 8 with notification management

jsandaruwan

J-Sandaruwan

Posted on November 14, 2021

Laravel 8 with notification management

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

Go to the project path and open the terminal.

cd bootstrap-app
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

php artisan make:notification PaymentsSave
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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

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

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']; 
}

Enter fullscreen mode Exit fullscreen mode

we want to edit the __construct function also that way.

public function __construct($notification)
{
   $this->notification = $notification;
}
Enter fullscreen mode Exit fullscreen mode

After we can edit the toDatabase function like that

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

Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Get Unread Notifications

$user->unreadNotifications;
Enter fullscreen mode Exit fullscreen mode

Get Read Notifications

$user->readNotifications;
Enter fullscreen mode Exit fullscreen mode

How to update status Read Notification

This one we want to pass the parameter of notification ID

$user->notifications->where('id', $id)->markAsRead();
Enter fullscreen mode Exit fullscreen mode

How to update status Unread Notification

$user->notifications->where('id', $id)->markAsUnread();
Enter fullscreen mode Exit fullscreen mode

How to update status All Read Notification

$user->notifications->markAsRead();
Enter fullscreen mode Exit fullscreen mode

How to update status All Unread Notification

$user->notifications->markAsUnread();
Enter fullscreen mode Exit fullscreen mode

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

Thank you,
J-Sandaruwan.

💖 💪 🙅 🚩
jsandaruwan
J-Sandaruwan

Posted on November 14, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Laravel 8 with notification management
laravel Laravel 8 with notification management

November 14, 2021