Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Laravel 8 with notification management
J-Sandaruwan
J-Sandaruwan

Posted on

     

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 modeExit 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:tablephp artisan migrate
Enter fullscreen modeExit fullscreen mode

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

php artisan make:notification PaymentsSave
Enter fullscreen modeExit fullscreen mode

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

<?phpnamespace App\Models;use Illuminate\Foundation\Auth\User as Authenticatable;use Illuminate\Notifications\Notifiable;class User extends Authenticatable{    use Notifiable;}
Enter fullscreen modeExit 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 modeExit 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 thevia 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. Thereforevia like this.

public function via($notifiable) {     return ['database']; }
Enter fullscreen modeExit fullscreen mode

we want to edit the__construct function also that way.

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

After we can edit thetoDatabase function like that

public function toDatabase($notifiable) {     return [ 'message' => $this->notification['message'] ?? '']; }
Enter fullscreen modeExit 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 modeExit fullscreen mode

Get Unread Notifications

$user->unreadNotifications;
Enter fullscreen modeExit fullscreen mode

Get Read Notifications

$user->readNotifications;
Enter fullscreen modeExit 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 modeExit fullscreen mode

How to update status Unread Notification

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

How to update status All Read Notification

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

How to update status All Unread Notification

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

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

Thank you,
J-Sandaruwan.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi, I’m @J-Sandaruwan (ජනිත් සඳරුවන්)
  • Location
    Hertfordshire
  • Joined

More fromJ-Sandaruwan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp