Laravel provides sending notifications across a variety of delivery channels like, including email, SMS (via Vonage, formerly known as Nexmo), and Slack. Sometimes we need to send notification to mulitple users in laravel. So I am here to show you how to send notification in laravel 9 application.
I will share with you a complete step by step guide like create notification, setup notification driver, send notification to multiple users etc in this laravel 9 notification tutorial. If you do not know how to send notification in laravel 9 then this example is for you.
So from this tutorial, you will learn laravel 9 send notification to multiple email address. So let's start the tutorial:
Step 1: Download laravel 9
In this first step, we need a fresh laravel app.
composer create-project laravel/laravel laravel-notification-system --prefer-dist
Step 2: Connect database
In this step, we will connect the database. So connect MySQL database like:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
If you are using MAMP local server in macOS; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in the .env file.
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Step 3: Create Notification Table
We need a notification table, so run the below command to create it:
php artisan notifications:table
It will generate notifications table like:
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}
Now run php artisan migrate
to create notifications
table.
Read also: Laravel 9 Database Notification Example
Step 4: Create Notification
To generate a notification, we have a make:notification
command. So run the below command to create a notification.
php artisan make:notification OffersNotification
To generate a notification with a corresponding Markdown template, you may use the --markdown
option of the make:notification
Artisan command:
php artisan make:notification OffersNotification --markdown=mail.invoice.paid
Now update this notification class like this:
app/Notifications/OffersNotification.php.
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class OffersNotification extends Notification
{
use Queueable;
private $offerData;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($offerData)
{
$this->offerData = $offerData;
}
/**
* 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)
->name($this->offerData['name'])
->line($this->offerData['body'])
->action($this->offerData['offerText'], $this->offerData['offerUrl'])
->line($this->offerData['thanks']);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'offer_id' => $this->offerData['offer_id']
];
}
}
Step 5: Create Route
Now update the route for notifications like:
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NotificationController;
Route::get('/send-notification', [NotificationController::class, 'sendOfferNotification']);
Step 6: Create Controller
Now in this step, we have to create a controller to process sending notifications in laravel 9. So create it like:
php artisan make:controller NotificationController
Now update this file like:
app/Http/Controllers/NotificationController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Notification;
use App\Notifications\OffersNotification;
class NotificationController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('product');
}
public function sendOfferNotification() {
$userSchema = User::first();
$offerData = [
'name' => 'John Doe',
'body' => 'You received an offer.',
'thanks' => 'Thank you',
'offerText' => 'Check out the offer',
'offerUrl' => url('/'),
'offer_id' => 007
];
Notification::send($userSchema, new OffersNotification($offerData));
dd('Task completed!');
}
}
Now run php artisan serve
and visit the below URL to check the tutorial for sending notifications in laravel 9.
Read also: Laravel Web Socket Example with Event Broadcasting
Hope it can help you.
#laravel #laravel-9x #notification