2026-04-03 07:57:40 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Filament\Notifications\Notification;
|
|
|
|
|
|
|
|
|
|
class NotificationService
|
|
|
|
|
{
|
|
|
|
|
public static function send(User|iterable $recipients, string $title, string $body, string $color = 'info', ?string $url = null): void
|
|
|
|
|
{
|
|
|
|
|
$notification = Notification::make()
|
|
|
|
|
->title($title)
|
|
|
|
|
->body($body)
|
|
|
|
|
->color($color);
|
|
|
|
|
|
|
|
|
|
if ($url) {
|
|
|
|
|
$notification->actions([
|
2026-04-03 08:51:47 +07:00
|
|
|
\Filament\Actions\Action::make('lihat')
|
2026-04-03 07:57:40 +07:00
|
|
|
->label('Lihat')
|
|
|
|
|
->url($url),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$notification->sendToDatabase(
|
|
|
|
|
$recipients instanceof User ? collect([$recipients]) : collect($recipients)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function toRole(string $role, string $title, string $body, string $color = 'info', ?string $url = null): void
|
|
|
|
|
{
|
|
|
|
|
$users = User::role($role)->get();
|
|
|
|
|
if ($users->isEmpty()) return;
|
|
|
|
|
self::send($users, $title, $body, $color, $url);
|
|
|
|
|
}
|
|
|
|
|
}
|