Laravel Notification System Example; In this tutorial we are going to share step by step guide on how to send a web notification in laravel application. This example show you how to create notification system in laravel application.
Laravel mechanism is getting stronger day by day, and we all know it. The laravel notification system not just supports mail but it also goes smooth with the database, broadcasting, sms, slack, markdown etc.
You can implement notifications system in you laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 apps.
Laravel 9|8 Notification Tutorial Example
Notifications can be seen as a short and straightforward message deliver to a user for giving vital info, events or to evoke action in the application. Preferably, notifications keep the existing users in the loop, and it boosts user engagement as well. This is also valuable from a user experience’s perspective.
Laravel Notification can be by mail, database, sms or slack. 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.
Step 1: Install Laravel App
First at all install laravel application using the following command.
composer create-project laravel/laravel laravel-notification-system --prefer-dist
Go into the app:
cd laravel-notification-system
Step 2: Set Up Database Connection
In this step we setup database credentials using adding in .env file and insert the given below code inside of it.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Step 3: Create Notification Table
In this step we will learn how to create a database table for storing the notification in laravel. Run the following command to produce notification table in the database.
php artisan notifications:table
Using the above command a new migration generated in your migrations with table columns name.
database\migrations\2022_02_05_042920_create_notifications_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
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();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notifications');
}
}
Afterwards, execute the command to migrate the notification table.
php artisan migrate
Step 4: Create Notification in Laravel
Now we need to create a notification using the laravel artisan command.
php artisan make:notification OrdersNotification
Above command generated a new file and a folder, the path of the Notification file is as follows:
app/Notifications/OrdersNotification.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class OrdersNotification extends Notification
{
use Queueable;
private $orderData;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($orderData)
{
$this->orderData = $orderData;
}
/**
* 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->orderData['name'])
->line($this->orderData['body'])
->action($this->orderData['orderText'], $this->orderData['orderUrl'])
->line($this->orderData['thanks']);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'order_id' => $this->orderData['order_id']
];
}
}
Step 5: Add Notification Route
Now we need to add a send notification route that helps to send a notification to a single user, open you routes/web.php and add a route just like below.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NotificationController;
Route::get('/send-notification', [NotificationController::class, 'sendOrderNotification']);
Step 6: Create Notification Controller
Here we need to create a new controller using the following command;
php artisan make:controller NotificationController
Open your notification controller and update the login here. We have send the order data to the user as a notification.
app/Http/Controllers/NotificationController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Notification;
use App\Notifications\OrdersNotification;
class NotificationController extends Controller
{
public function index()
{
return view('order');
}
public function sendOrderNotification() {
$user = User::first();
$orderData = [
'name' => 'Order Name',
'body' => 'You received an order.',
'thanks' => 'Thank you',
'orderText' => 'Check out the order',
'orderUrl' => url('/'),
'order_id' => 006
];
Notification::send($user, new OrdersNotification($orderData));
dd('Task completed!');
}
}
Once you are done with everything, then you can run given below command to start the laravel app on the browser and send the test notifications.
php artisan serve
The notification system can be tested with the following path.
http://127.0.0.1:8000/send-notification
How to Send Notification in Laravel App
Do you know laravel notification system offers two methods to send notifications. You may opt either Notifiable trait or Notification facade. Preferably, in this tutorial, we will discuss the Notifiable trait.
Generically, the trait is a single method default work with App\User model. Often, used for sending a notification: notify, and it demands to receive a notification instance.
use App\Notifications\OrdersNotification;
$user->notify(new OrdersNotification($orderData));
Here is the simplest way to receive a notification.
dd($user->notifications);
I hope laravel notification system example tutorial step by step working for you. So guys today you have did how to create notification system in laravel 9 application.