A Notification is used to send messages to users.
Real-Life Example
Suppose you order a product.
You receive:
- Order Placed
- Order Shipped
- Order Delivered
These are notifications.
Example:
Step 1: Create Notification:
php artisan make:notification OrderPlacedapp/
└── Notifications/
└── OrderPlaced.phpStep 2: Notification Class
app/Notifications/OrderPlaced.php
class OrderPlaced extends Notification
{
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Order Placed')
->line('Your order has been placed successfully.');
}
}via() → Specifies how to send the notification (mail, database, sms, etc.).
toMail() → Defines the email content.
Step 3: Send Notification:
UserController.php
use App\Notifications\OrderPlaced;
$user->notify(new OrderPlaced());Laravel Notifications provide a simple way to send messages to users through different channels such as email, SMS, database, Slack, and broadcast. They are commonly used for order updates, password resets, and welcome messages.