Laravel provides a robust and easy-to-use notification system that supports sending notifications through various channels, including email, SMS, and Slack. Here’s a step-by-step guide on how to send notifications in a Laravel 11 application.
Step-by-Step Guide to Sending Notifications in Laravel 11
1. Create a Notification
You can create a notification using the make:notification
Artisan command. This command will generate a new notification class in the app/Notifications
directory.
php artisan make:notification InvoicePaid
This will create a new notification class InvoicePaid.php
with a basic structure.
2. Define Notification Channels
Open the generated InvoicePaid
notification class and define the channels through which you want to send the notification. By default, the via
method specifies the channels.
// app/Notifications/InvoicePaid.php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class InvoicePaid extends Notification
{
use Queueable;
private $invoice;
public function __construct($invoice)
{
$this->invoice = $invoice;
}
/**
* 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)
->line('Your invoice has been paid.')
->action('View Invoice', url('/invoices/' . $this->invoice->id))
->line('Thank you for your business!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'invoice_id' => $this->invoice->id,
'amount' => $this->invoice->amount,
];
}
}
- via: This method specifies the channels through which the notification will be delivered. In this example, the notification will be sent via email and stored in the database.
- toMail: This method builds the email representation of the notification.
- toArray: This method defines the data to be stored in the database if the
database
channel is used.
3. Sending Notifications
You can send notifications to a notifiable entity, which is typically a user. The Notifiable
trait provides methods to send notifications. Add the Notifiable
trait to your User model:
// app/Models/User.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
// ...
}
To send the notification, use the notify
method:
// In a controller or any other place
use App\Models\User;
use App\Notifications\InvoicePaid;
// Retrieve a user to notify
$user = User::find(1);
// Retrieve the invoice
$invoice = Invoice::find(1);
// Send the notification
$user->notify(new InvoicePaid($invoice));
4. Database Notifications
To store notifications in the database, you need to create a notifications table. Run the notifications
table migration:
php artisan notifications:table php artisan migrate
The toArray
method in the notification class will store the returned array in the data
column of the notifications
table.
5. Configuring Mail Notifications
Ensure your mail configuration is set up in the .env
file:
MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS="hello@example.com" MAIL_FROM_NAME="${APP_NAME}"
Replace these values with your mail server details.
6. Viewing Notifications in a View
You can fetch notifications for a user and display them in a view. For example:
// In a controller
use Illuminate\Support\Facades\Auth;
public function showNotifications()
{
$notifications = Auth::user()->notifications;
return view('notifications', compact('notifications'));
}
blade<!-- resources/views/notifications.blade.php --> <ul> @foreach ($notifications as $notification) <li>{{ $notification->data['message'] }}</li> @endforeach </ul>
Conclusion
By following these steps, you can easily integrate and send notifications using various channels in your Laravel 11 application. Laravel's notification system is powerful and flexible, allowing you to notify users through email, database, SMS, Slack, and other channels effortlessly.
0 Comments