Laravel 9 Database Notification Example
Hello artisan,
In this laravel 9 database notification example, I will share with you a complete guide on how to send notification to multiple users in laravel using database notification. If you don't know how to send notification in laravel, then this example is going to be for you. I will show you step by step, so that you can understand it clearly.
Laravel provides database driver to send notification in laravel. So before sending notification with database driver, we have to change the driver sync
to database
and we need a notification table if we use database notifications.
With laravel 9 database notifications, we can send email, send sms, send slack message notification to multiple users. In this example I give you a very simple way to create the first notification to send mail in laravel 9. we can create notifications by laravel artisan command like make:notification
command. Let's see the database notification tutorial.
Step 1: Install Laravel 9
I am going to explain step by step from scratch so, we need to get a fresh Laravel 8 application using the bellow command, So open your terminal OR command prompt and run the bellow command to download a fresh Laravel project.
composer create-project --prefer-dist laravel/laravel Notification
Step 2: Create Database Table
In this step, we need to create a "notifications" table by using laravel 9 artisan command, so let's run below command:
php artisan notifications:table //We need it for Database notification
Read aslo Laravel Vue Js Read Data Using Vuex Example From Scratch
Now run php artisan migrate command to generate notifications table:
php artisan migrate
Step 3: Create Notification
In this step, we need to create "Notification" by using laravel 9 artisan command, so let's run the bellow command, we will create TaskComplete
php artisan make:notification TaskComplete
.now you can see a new folder will create as "Notifications" in the app folder. You need to make the following changes as like below class.
app/Notifications/TaskComplete.php
namespace App\Notifications;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class TaskComplete extends Notification
{
use Queueable;
private $details;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
public function via($notifiable)
{
return ['mail','database'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting($this->details['greeting'])
->line($this->details['body'])
->line($this->details['thanks']);
}
public function toDatabase($notifiable)
{
return [
'data' => $this->details['body']
];
}
}
Step 4: Create Controller
We don't need a controller, but you can use a controller. So just go to web.php and paste the following code.
Read also : Sending Notification Via Laravel Queue Example From Scratch
routes/web.php
//Notify Route
Route::get('/notify', function () {
$user = \App\User::find(1);
$details = [
'greeting' => 'Hi Artisan',
'body' => 'This is our example notification tutorial',
'thanks' => 'Thank you for visiting codechief.org!',
];
$user->notify(new \App\Notifications\TaskComplete($details));
return dd("Done");
});
Now we are ready to send the first notification to the user. We can check when a user reads their notification. So for doing it just see below code example.
Step 5 : Mark as Read
We can easily create mark as read notifications in laravel 9 using the markAsRead() method like:
Route::get('/markAsRead', function(){
auth()->user()->unreadNotifications->markAsRead();
return redirect()->back();
})->name('mark');
Here we fetch authenticated user instance and then access his all unread notification and give a route to it. So that when a user clicks it then it should be marked as read in the database. only for it in the above, we make a notification table.
Now see the below code example to understand clearly.
{{ auth()->user()->unreadNotifications->count() }}
//Mark all as read notification using {{ route('mark') }} route
@foreach(auth()->user()->unreadNotifications as $notification)
{{ $notification->data['data'] }}
@endforeach
you can run the following URL to send notification.
http://localhost:8000/notify
Hope it will be helpful for you. You can also find which read notifications below.
Read also : Laravel Notification Example From Scratch
We can easily get all the read notifications in laravel 9 using the like:
@foreach(auth()->user()->readNotifications as $read)
{{ $read->data['data'] }}
@endforeach
Hope it can help you.