PHP code example of xjoc / laravel-notification-center

1. Go to this page and download the library: Download xjoc/laravel-notification-center library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xjoc / laravel-notification-center example snippets




declare(strict_types=1);

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use XJOC\NotificationCenter\Concerns\HasNotificationCenter;
use XJOC\NotificationCenter\Contracts\NotifiableNotification;

final class OrderConfirmedNotification extends Notification implements NotifiableNotification
{
    use HasNotificationCenter;

    public function __construct(
        private string $orderId,
        private string $total,
    ) {}

    public function notificationType(): string
    {
        return 'order.confirmed';
    }

    /** @return array<string, mixed> */
    public function notificationVariables(object $notifiable): array
    {
        return [
            'customer_name' => $notifiable->name,
            'order_id'      => $this->orderId,
            'total'         => $this->total,
        ];
    }
}

$user->notify(new OrderConfirmedNotification($orderId, $total));

public function injectTemplate(string $channel, string $rendered, ?string $subject = null): void;

'types' => [
    'order.confirmed' => [
        'name'      => 'Order Confirmed',
        'category'  => 'transactional',
        'channels'  => ['mail', 'whatsapp'],
        'locked'    => false,
        'variables' => ['customer_name', 'order_id', 'total'],
    ],
    'otp.sent' => [
        'name'      => 'OTP Sent',
        'category'  => 'essential',
        'channels'  => ['whatsapp'],
        'locked'    => true,
        'variables' => ['otp_code', 'expires_in'],
    ],
],

$user->notify(new OrderConfirmedNotification($orderId, $total));

use XJOC\NotificationCenter\Facades\NotificationCenter;

NotificationCenter::send(
    typeKey: 'order.confirmed',
    notifiables: $user,                 // a single notifiable or an iterable of them
    variables: ['customer_name' => 'Sam', 'order_id' => '42', 'total' => '$10'],
    channels: null,                     // null = all supported channels for the type
);

public function send(
    string $typeKey,
    iterable|object $notifiables,
    array $variables = [],
    ?array $channels = null,
): void;



declare(strict_types=1);

namespace App\Events;

use XJOC\NotificationCenter\Contracts\ProvidesNotificationContext;

final class OrderWasConfirmed implements ProvidesNotificationContext
{
    public function __construct(private object $customer, private string $orderId) {}

    /** @return iterable<int, object> */
    public function notificationRecipients(): iterable
    {
        return [$this->customer];
    }

    /** @return array<string, mixed> */
    public function notificationVariables(): array
    {
        return ['customer_name' => $this->customer->name, 'order_id' => $this->orderId];
    }
}
bash
php artisan notification-center:install
bash
php artisan vendor:publish --tag="notification-center-config"
php artisan migrate
php artisan notification-center:sync
bash
php artisan vendor:publish --tag="notification-center-config"
bash
  php artisan notifications:table
  php artisan migrate