Laravel 8.x Notification Tutorial | Create Notification With Laravel

Hello artisan, in this tutorial, i will show you how to send email notification in laravel 8. we will create laravel 6 notification to email address. we will send email to notify user using laravel 8 notification system.

Using laravel 8 notifications, you can send email, send sms, send slack message notification to user. in this example i give you very simple way to create first notification to send mail in laravel 8. we can easily create Notification by laravel artisan command.

We can easily customization of notification like mail subject, mail body, main action etc. we almost require to use notification when we work on large amount of project like e-commerce. might be you need to send notification for payment receipt, order place receipt, invoice etc.

In this example we will create email notification and send it to particular user, than we saved to database. So, you need to follow few step to make basic example with notification.

In this section we will see laravel database notification with mark as read notification. I will also show you mark as unread that mean we will find out which notification are not read yet. We will find out laravel unread notification using laravel database notification. Let's see the laravel notification example.

laravel-6-notifications

 

Step 1: Install Laravel 8

I am going to explain step by step from scratch so, we need to get fresh Laravel 8 application using bellow command, So open your terminal OR command prompt and run bellow command to download fresh Laravel project.

composer create-project --prefer-dist laravel/laravel Notification

 

Step 2: Create Database Table

In this step, we need to create "notifications" table by using laravel 5 artisan command, so let's run bellow command:

php artisan notifications:table  //We need it for Database notification

 

Read aslo Laravel Vue Js Read Data Using Vuex Example From Scratch

 

php artisan migrate

 

Step 3: Create Notification

In this step, we need to create "Notification" by using laravel 8 artisan command, so let's run bellow command, we will create TaskComplete

php artisan make:notification TaskComplete

 

.now you can see new folder will create as "Notifications" in app folder. You need to make following changes as like bellow 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 controller, but you can use controller. So just go web.php and paste those 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 first notification to user. We can check when a user read their notification. So for doing it just see below code example.

 

Step 5 :  Mark as Read

 

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 click it then it should be marked as read in database. only for it in the above we make notification table.

 

Now see the below code example to more 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 following url to send notification.

http://localhost:8000/notify

 

Hope it will be helpful for you. You can also find which are read notification like below.

 

Read also : Laravel Notification Example From Scratch

 

@foreach(auth()->user()->readNotifications as $read)

       {{ $read->data['data'] }}

@endforeach

 

Now you can check it. If you face any issues then feel free to comment and don't forget to share with your friends.

 

#laravel #laravel-6 #email #notifications #database-notifications