Back to Course |
Laravel User Timezones Project: Convert, Display, Send Notifications

Creating Laravel Notification Class

We need some kind of Notification to send. For this, we'll create a "reminder" Notification that will be sent 1 hour before the booking.

Notice: It will not contain any styling/information as we are focusing on the Notification scheduling part, and not the Notifications themselves.


Creating the Notification

We can get this quickly done by using the php artisan make:notification command:

php artisan make:notification BookingReminder1H

And then we will edit all the Notifications to accept the Booking Model as a parameter:

app/Notifications/BookingReminder1H.php

use App\Models\Booking;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
 
class BookingReminder1H extends Notification
{
public function __construct(protected Booking $booking)
{
}
 
public function via($notifiable): array
{
return ['mail'];
}
 
public function toMail($notifiable): MailMessage
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
 
public function toArray($notifiable): array
{
return [];
}
}

This is it, we have our Notification ready to be sent out. Let's move on to the next step and schedule them!