Skip to content

Notifiable Entities ​

Learn how to configure entities and models to receive notifications and manage in-app alerts.


The Notifiable Trait ​

To allow any CodeIgniter 4 Model or Entity to receive notifications, import the Jengo\Notifications\Concerns\Notifiable trait:

php
namespace App\Entities;

use Jengo\Base\Entities\BaseEntity;
use Jengo\Notifications\Concerns\Notifiable;

class User extends BaseEntity
{
    use Notifiable;

    /**
     * Custom route for SMS notifications.
     */
    public function routeNotificationForSms(): ?string
    {
        return $this->attributes['phone_number'] ?? null;
    }

    /**
     * Custom route for WebPush/FCM notifications.
     */
    public function routeNotificationForPush(): ?string
    {
        return $this->attributes['fcm_token'] ?? null;
    }
}

Routing Customization ​

By default, the notification engine checks standard property names on your entity to find the recipient address:

ChannelDefault Entity AttributeCustom Route Method
mail$entity->emailrouteNotificationForMail(): string|array
sms$entity->phone_number, $entity->phone, $entity->mobilerouteNotificationForSms(): string
push$entity->device_token, $entity->fcm_token, $entity->push_subscriptionrouteNotificationForPush(): string|array
slack$entity->slack_webhook_urlrouteNotificationForSlack(): string
webhook$entity->webhook_urlrouteNotificationForWebhook(): string

Example: Multi-Recipient Mail Routing ​

php
public function routeNotificationForMail(): array
{
    return [$this->email, 'backup-' . $this->email];
}

Managing In-App Database Notifications ​

The Notifiable trait includes helper methods to retrieve and mutate notifications stored in the database:

Querying Notifications ​

php
// Retrieve all notifications
$all = $user->notifications();

// Retrieve only unread notifications
$unread = $user->unreadNotifications();

// Retrieve only read notifications
$read = $user->readNotifications();

Accessing Alert Details ​

Each item returned is an instance of Jengo\Notifications\Entities\DatabaseNotification:

php
foreach ($user->unreadNotifications() as $notification) {
    echo $notification->title();       // Title string
    echo $notification->message();     // Alert message
    echo $notification->link();        // Actionable URL
    echo $notification->actionText();  // Button label
    echo $notification->level();       // 'info', 'success', 'warning', 'danger'
    echo $notification->created_at;    // Timestamp

    // Mark single notification as read
    $notification->markAsRead();
}

Bulk Marking as Read ​

php
// Mark all unread notifications for this user as read
$user->markAsRead();

Conditional Delivery (shouldSend) ​

You can control whether a notification should be delivered to a specific recipient or channel by overriding shouldSend() on the Notification class:

php
class MonthlySummary extends Notification
{
    public function via(object $notifiable): array
    {
        return ['mail', 'database', 'sms'];
    }

    public function shouldSend(object $notifiable, string $channel): bool
    {
        // Don't send SMS to users who have opted out in user preferences
        if ($channel === 'sms' && ! ($notifiable->sms_enabled ?? false)) {
            return false;
        }

        return true;
    }
}

On-Demand (Ad-Hoc) Notifiables ​

When you need to deliver a notification to a recipient who does not exist in your database table (such as a guest customer or external billing endpoint), use Notification::route():

php
use Jengo\Notifications\Notification;

Notification::route('mail', 'guest@example.com')
    ->route('sms', '+254700000000')
    ->notify(new OrderConfirmation($order));

Released under the MIT License.