Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

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

How to Crawl any website Meta Title and Meta Description in Laravel ?

1st step install below package. Next to create Controller First go to route and put below code Next go to controller and put below code Next go…

SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: insert into `oauth_clients` (`user_id`, `name`, `secret`

In this tutorial i’m going to solve the error SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: insert into oauth_clients (user_id, name, secret Error :-…

Top 20 Laravel Interview Question in 2024

In this tutorial im going share interview experience For laravel developer. A list of top frequently asked Laravel Interview Questions and answers are given below. Q #1) What is…

How to Get Google Analytics API key ?

In this tutorial we’re going to share how to get the google Analytics API key. I have shared in very easy way. First go enable Google analytics…

Youtube Subscriber Count in ReactJs

In this tutorial i’m going to learn how to count YouTube Subsribers count, views count as well, as define below. In order to install your app, first…

How to Disable Laravel’s Eloquent timestamps in laravel ?

In this tutorial we’re going to share how to disable the automatic created_at and updated_at timestamps in Laravel’s Eloquent models, along with explanations of different scenarios: 1st…

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] How to send Notifications With Database In Laravel ? […]

1
0
Would love your thoughts, please comment.x
()
x