Laravel Fast2SMS provides two notification channels out of the box: fast2sms for SMS and whatsapp for WhatsApp.
Return 'fast2sms' from your notification's via() method:
use Illuminate\Notifications\Notification;
use Shakil\Fast2sms\Enums\SmsRoute;
use Shakil\Fast2sms\Notifications\Messages\SmsMessage;
class OrderShipped extends Notification
{
public function __construct(private readonly Order $order) {}
public function via(object $notifiable): array
{
return ['fast2sms'];
}
public function toSms(object $notifiable): SmsMessage
{
return SmsMessage::create("Your order #{$this->order->id} has shipped!")
->withRoute(SmsRoute::QUICK);
}
}
Add routeNotificationForFast2sms() to your notifiable model:
use Illuminate\Notifications\Notifiable;
class User extends Model
{
use Notifiable;
public function routeNotificationForFast2sms(): string
{
return $this->phone_number;
}
}
$user->notify(new OrderShipped($order));
// Or via the Notification facade
Notification::send($users, new OrderShipped($order));
SmsMessage::create('Your message here')
->withRoute(SmsRoute::QUICK) // Set SMS route
->withNumbers(['9876543210']) // Override recipient numbers
->from('MYAPP'); // Override sender ID
| Old (deprecated) | New |
|---|---|
content('...') |
withContent('...') |
route(...) |
withRoute(...) |
to('...') |
withNumbers('...') |
use Illuminate\Notifications\Notification;
use Shakil\Fast2sms\Notifications\Messages\WhatsAppMessage;
class OrderShipped extends Notification
{
public function via(object $notifiable): array
{
return ['whatsapp'];
}
public function toWhatsApp(object $notifiable): WhatsAppMessage
{
return WhatsAppMessage::text("Your order #{$this->order->id} has shipped!");
}
}
public function routeNotificationForWhatsapp(): string
{
return $this->phone_number;
}
// Text
WhatsAppMessage::text('Hello!');
// Image
WhatsAppMessage::image('https://example.com/image.jpg');
// Document
WhatsAppMessage::document('https://example.com/file.pdf');
// Location
WhatsAppMessage::forLocation(lat: 28.6139, lng: 77.2090);
// Interactive
WhatsAppMessage::forInteractive([/* ... */]);
public function via(object $notifiable): array
{
return ['fast2sms', 'whatsapp', 'mail'];
}
How can I help you explore Laravel packages today?