Laravel 9 Notification | Send Notification In Laravel

Hello artisan,

In this Laravel notification tutorial, I will show you a step by step guide that how to send notifications in Laravel 9 to specific email addresses. I will give you a complete example of the Laravel 9 send notification process. If you don't know how to send notifications in Laravel 9 then this example is for you. 

I will show you a simple way of sending notifications in Laravel 9. It is very easy to send notifications in the Laravel framework.  You know that Laravel provides an easy way of sending an email using the Notification class that is easily sent to any delivery channels such as email, SMS, and Slack. 

In Laravel 9, every notification is represented by a single class that is stored in the app/Notifications directory. We need to run the below command to create a notification. Laravel provides an artisan command to create a notification like:

make:notification

 

We can send notifications using notifiable traits and Notification facades. We will see both examples in this tutorial. Let's see the example of how to send notification in Laravel 9 app:

 

Step 1: Laravel Installation

If you don't have a Laravel 9 installed in your local just run the following command below:

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

 

Step 2: Migration Setup

In this step, we have to generate first the notifications table before running the migrations. Kindly run the following command to generate notification table:

php artisan notifications:table

//then

php artisan migrate

 

Step 3: Create Notification

There is an artisan command to create notifications. Run the below command to create a notification class:

php artisan make:notification EmailNotification

 

Now update the notification like this:

app\Notifications\EmailNotification.php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class EmailNotification extends Notification
{
    use Queueable;

    /**
     * @var array $project
     */
    protected $project;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($project)
    {
        $this->project = $project;
    }

    /**
     * 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->project['greeting'])
                    ->line($this->project['body'])
                    ->action($this->project['actionText'], $this->project['actionURL'])
                    ->line($this->project['thanks']);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        return [
            'project_id' => $this->project['id']
        ];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

 

Read also: Laravel 8.x Notification Tutorial | Create Notification with Laravel

 

Step 4: Add routes

Now in this step, we have to create route. So update route like this:

routes/web.php

use App\Http\Controllers\TestController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/send', [TestController::class,'send'])->name('home.send');

 

Step 5: Update Controller

Now update the test controller like the below:

app\Http\Controllers\TestController.php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use App\Notifications\EmailNotification;
use Illuminate\Support\Facades\Notification;

class TestController extends Controller
{
    public function send() 
    {
    	$user = User::first();
  
        $project = [
            'greeting' => 'Hi '.$user->name.',',
            'body' => 'This is the project assigned to you.',
            'thanks' => 'Thank you this is from codecheef',
            'actionText' => 'View Project',
            'actionURL' => url('/'),
            'id' => 57
        ];
  
        Notification::send($user, new EmailNotification($project));
   
        dd('Notification sent!');
    }
}

 

Now all are set to go. Now if you visit this url, then a notification will be sent to the first users.

 

url
http://127.0.0.1:8000/send

 

Read also: Laravel Web Socket Example with Event Broadcasting

 

Hope it can help you to send notifications in your Laravel 9 applications.

 

#laravel #laravel-9x #laravel-notification