How to send Notifications With Database In Laravel ?

In this tutorial we’re going to learn how to send notification if someone register on our software then send him notification.

Install laravel project

composer create-project laravel/laravel:^9.0 notification_email

Next to create migration file using below command

php artisan notification:table

Next migrate the table

php artisan migrate

Next to generate authentication

composer require laravel/ui
php artisan ui bootstrap
php artisan ui bootstrap --auth

Next to run below command

npm install && npm run dev

Next to start the server

php artisan serve

And do register and login

Next to run below command

php artisan make:notification UserFollowNotification

Next go to UserFollowNotification and put below code

<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class UserFollowNotification extends Notification
{
    use Queueable;
    public $user;
    /**
     * Create a new notification instance.
     */
    public function __construct($user)
    {
        $this->user = $user;
    }
    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['database'];
    }
    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }
    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            'user_id'=> $this->user['id'],
            'name'=> $this->user['name'],
            'email'=> $this->user['email']
        ];
    }
}

Next go to profilecontroller and put below code

public function notify(){
        if (auth()->user()) {
            $user = auth()->user();
            Log::info("user me kya aa rha hai: " . $user);
            auth()->user()->notify(new UserFollowNotification($user));

        }
        dd('done');

    }

Next go to web.php and put below code

Route::get('/notify', [ProfileController::class, 'notify']);

Next to hit this route

Route::get('/notify', [ProfileController::class, 'notify']);

Next go to blade page where you want to show the notification I have put the dashboard.blade.php

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Dashboard') }}
        </h2>
    </x-slot>
    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900 bg-white border-b" style="color: blue;">

                    @foreach (auth()->user()->unreadnotifications as $notification)
                    <div class="bg-blue-300 p-3 m-2">
                       <b> {{ $notification->data['name']}}</b> started following you !!!
                       <a href="{{ route('marksread',$notification->id)}}" class="p-2 bg-red-400 rounded-lg" style="color: red;">mark as read</a>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
</x-app-layout>

Next go to web.php and put below code for mark as unread message

Route::get('/marksread/{id}', [ProfileController::class, 'markasread'])->name('marksread');
public function markasread($id){
        if($id){
            auth()->user()->unreadNotifications->where('id',$id)->markAsRead();
        }
        return back();
    }

Next run the project and login

Now notification coming successfully.

Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

Could not fetch https://api.github.com/repos/Setasign/FPDI/zipball/

When I composer update in Laravel its throwing error could not fetch issues so let’s start to finish this issues. 1st step go to GitHub setting section….

Error: error:0308010C:digital envelope routines::unsupported

In this tutorial I’m going to share how to solve Error: error:0308010C:digital envelope routines::unsupported. Just copy below code and run your terminal. After run above all code…

API calls from the server require an appsecret_proof argument

In this tutorial im going to solve this issue API calls from the server require an appsecret_proof argument. 1st step go to developer facebook advance setting and…

How to Delete All Records Older Than 30 Days in Laravel 10 ?

In Laravel, you can use the Eloquent ORM to delete records older than a certain number of days. Here’s an example of how you can delete all…

How to safely remove Laravel Debugbar ?

What is Laravel Debugbar ? Laravel Debugbar is a package for the Laravel PHP framework that provides a debug bar or toolbar for your application. It offers…

Example of __construct and __destruct in laravel ?

In Laravel, PHP classes can have a constructor and destructor like any other PHP class. The constructor is called when an object is created, and the destructor…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x