How to Send Mail Notification in Laravel 6?

In this article, we will share with you how to create a notification in laravel 6 and how to send it to users. laravel provides much helpful functionality by default. sometimes we need to send some information by notification to system users we can do it by laravel Notification class and functions().

Here, we will create one simple notification to our laravel application send it to a user by mail or also store some related information into the databases.

Where to use Laravel Notification?

Many laravel beginners has one question were to use laravel notification in our laravel application. so, we i will give you some real field examples for laravel notification functionality. like when we work with web-based ERP and send users some invoices as well as store some related data into the database then notification it will be very helpful. laravel provide to developer many ways to send notification by notification class 1.) mail, 2.) store into a database, 3.) by broadcast (use for real-time notification by pusher and Redis), 4.) slack

All Steps

Step - 1 : Create Laravel Application

In the first step, we need to create one new fresh laravel 6 application in our local system help by running the following command in your terminal.

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

Step - 2 : Create Migration

In step two we need to create notifictions table by the following command into the terminal on the project root directory.

php artisan notifications:table

After the create notifications table migration then run the laravel migration by the following command.

php artisan migrate

Step - 3 : Create Notification Class

In the third step, we need to create the notification class by running the following command in the terminal.

php artisan make:notification DemoNotification

After, run the above command in the terminal then notification file created at aap/Notifications/DemoNotification.php

<?php
   
namespace App\Notifications;
   
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
   
class DemoNotification extends Notification
{
    use Queueable;
  
    private $details;
   
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }
   
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail','database'];
    }
   
    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->greeting($this->details['greeting'])
            ->line($this->details['body'])
            ->action($this->details['actionText'], $this->details['actionURL'])
            ->line($this->details['thanks']);
    }
  
    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        return [
            'order_id' => $this->details['order_id']
        ];
    }
}

Step - 4 : Create Route

In this step, we need to create a route to send notifications to the user. open file routes/web.php file and create the following route.

Route::get('send', 'NotificationController@send');

Step - 5 : Create Controller

Now, we need to create app/Http/Controllers/NotificationController.php file by run the following command in the terminal.

php artisan make:controller NotificationController

Now, open the app/Http/Controllers/NotificationController.php and write the following code into it.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
use Notification;
use App\Notifications\DemoNotification;
  
class NotificationController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
  
    public function send(Request $request)
    {
        $user = User::first();
  
        $details = [
            'greeting' => 'Hi Artisan',
            'body' => 'This is my first notification from HackTheStuff',
            'thanks' => 'Thank you for using HackTheStuff article!',
            'actionText' => 'View My Site',
            'actionURL' => url('https://hackthestuff.com'),
            'order_id' => 'Order-20190000151'
        ];
  
        Notification::send($user, new DemoNotification($details));
   
        dd('Your notification send seuccessfully!');
    }  
}

You can also send a notification the following way.

$user->notify(new MyFirstNotification($details));

And, you can get send notification by following command.

dd($user->notifications);

Conclusion

You can see send notification in laravel is very easy to use. You can check I hope you will like this article. Also please follow us on Twitter and Like our Facebook page for updates.

Tags: