Quick Start
Learn how to generate, define, and send notifications in your application.
1. Generating Notifications
Use the Spark generator command to scaffold a new notification class in app/Notifications/:
bash
php spark make:notification OrderShippedThis generates app/Notifications/OrderShipped.php.
2. Defining the Notification
A notification class extends Jengo\Notifications\Notification and defines:
via(object $notifiable): array- Determines which channels the notification should be delivered to.- Builder methods for each channel, such as
toMail(),toDatabase(),toSms(),toPush(),toSlack(),toWebhook(), ortoBroadcast().
php
namespace App\Notifications;
use Jengo\Notifications\Messages\DatabaseMessage;
use Jengo\Notifications\Messages\MailMessage;
use Jengo\Notifications\Messages\SmsMessage;
use Jengo\Notifications\Notification;
class OrderShipped extends Notification
{
public function __construct(public array $order)
{
parent::__construct();
}
/**
* Determine target delivery channels.
*/
public function via(object $notifiable): array
{
return ['mail', 'database', 'sms'];
}
/**
* Mail representation.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage())
->subject("Order #{$this->order['id']} Has Shipped!")
->greeting("Hello {$notifiable->name},")
->line("Your order has been shipped via {$this->order['carrier']}.")
->line("Tracking Number: {$this->order['tracking_number']}")
->action('Track Shipment', site_url("orders/{$this->order['id']}/track"))
->salutation('Thank you for shopping with us!');
}
/**
* Database (In-App) representation.
*/
public function toDatabase(object $notifiable): DatabaseMessage
{
return (new DatabaseMessage())
->title('Order Shipped')
->message("Your order #{$this->order['id']} is on its way.")
->link(site_url("orders/{$this->order['id']}"))
->actionText('View Order')
->level('info');
}
/**
* SMS representation.
*/
public function toSms(object $notifiable): SmsMessage
{
return (new SmsMessage("Order #{$this->order['id']} shipped! Track: {$this->order['tracking_url']}"));
}
}3. Sending Notifications
Notifications can be dispatched through multiple approaches:
Direct via Notifiable Entity
Any model or entity using the Notifiable trait provides the notify() method:
php
$user = $userModel->find($userId);
$user->notify(new OrderShipped($order));Via Global notify() Helper
php
notify($user, new OrderShipped($order));Static Dispatch via Notification::send()
Send to multiple recipients simultaneously:
php
use Jengo\Notifications\Notification;
Notification::send([$customer, $supportTeam], new OrderShipped($order));On-Demand Ad-Hoc Routing
Send notifications to recipients that are not saved in your database (e.g., ad-hoc emails or phone numbers):
php
use Jengo\Notifications\Notification;
Notification::route('mail', 'accounting@partner.com')
->route('sms', '+254712345678')
->notify(new OrderShipped($order));Or using the notification() helper:
php
notification('mail', 'accounting@partner.com')->notify(new OrderShipped($order));