Database (In-App) Channel
The database channel stores notifications directly in your database, powering notification bells, alert center drawers, and in-app activity feeds.
The DatabaseMessage Builder
Define a toDatabase() method in your notification class returning a Jengo\Notifications\Messages\DatabaseMessage:
php
namespace App\Notifications;
use Jengo\Notifications\Messages\DatabaseMessage;
use Jengo\Notifications\Notification;
class PaymentReceived extends Notification
{
public function __construct(public array $payment)
{
parent::__construct();
}
public function via(object $notifiable): array
{
return ['database'];
}
public function toDatabase(object $notifiable): DatabaseMessage
{
return (new DatabaseMessage())
->title('Payment Received')
->message("Successfully received \${$this->payment['amount']} from {$this->payment['customer']}.")
->link(site_url("payments/{$this->payment['id']}"))
->action('View Receipt', site_url("payments/{$this->payment['id']}"))
->level('success')
->icon('check-circle')
->data([
'payment_id' => $this->payment['id'],
'currency' => 'USD',
]);
}
}Message Properties
title(string $title): Alert title or heading.message(string $message): Descriptive alert body.link(string $url, ?string $text = null): Action URL linked to the notification.action(string $text, string $url): Call-to-action button label and URL.level(string $level): Severity rating ('info','success','warning','error').icon(string $icon): Icon identifier for UI display (e.g.'bell','shield','credit-card').data(array $data): Any extra metadata serialized with the notification.
Displaying In-App Alerts in Views / APIs
When using the Notifiable trait, retrieve notifications directly from the recipient:
php
// In a Controller
public function index()
{
$user = auth()->user();
return view('notifications/index', [
'unread' => $user->unreadNotifications(),
'all' => $user->notifications(),
]);
}JSON API / Inertia Props Example
php
public function unread()
{
$notifications = auth()->user()->unreadNotifications();
return response()->json([
'count' => count($notifications),
'items' => array_map(function ($n) {
return [
'id' => $n->id,
'title' => $n->title(),
'message' => $n->message(),
'link' => $n->link(),
'actionText' => $n->actionText(),
'level' => $n->level(),
'created_at' => $n->created_at,
];
}, $notifications),
]);
}Marking As Read
php
// Mark an individual notification as read
$notification = $user->notifications()->find($notificationId);
$notification->markAsRead();
// Mark all unread notifications for user as read
$user->markAsRead();