Yiğit Kerem
Yiğit Kerem
FFilament
Created by Yiğit Kerem on 8/16/2024 in #❓┊help
Deliver notifications in email
Thank you for the awesome project and the help. Take care!
17 replies
FFilament
Created by Yiğit Kerem on 8/16/2024 in #❓┊help
Deliver notifications in email
This is how I solved it for future reference for anybody who needs it. I'm not sure if it is the cleanest way of doing things but I would however believe such a simple addition to the upstream could be helpful in many cases. Is there any reason it is neglected?
17 replies
FFilament
Created by Yiğit Kerem on 8/16/2024 in #❓┊help
Deliver notifications in email
<?php

public function boot(): void
{
.
.
.

Notification::macro('toEmail', function (Collection|User $users): void {
if ($users instanceof User) {
$users->notify(new FilamentNotificationEmail($this));
} else {
foreach ($users as $user) {
$users->notify(new FilamentNotificationEmail($this));
}
}
});

}
<?php

public function boot(): void
{
.
.
.

Notification::macro('toEmail', function (Collection|User $users): void {
if ($users instanceof User) {
$users->notify(new FilamentNotificationEmail($this));
} else {
foreach ($users as $user) {
$users->notify(new FilamentNotificationEmail($this));
}
}
});

}
17 replies
FFilament
Created by Yiğit Kerem on 8/16/2024 in #❓┊help
Deliver notifications in email
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class FilamentNotificationEmail extends Notification
{
use Queueable;

protected \Filament\Notifications\Notification $filament_notification;

/**
* Create a new notification instance.
*/
public function __construct(\Filament\Notifications\Notification $filament_notification)
{
$this->filament_notification = $filament_notification;
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{

$action = $this->filament_notification->getActions();
$action = $action[0] ?? null;

return (new MailMessage)
->subject($this->filament_notification->getTitle())
->greeting('Hello ' . $notifiable->name . ',')
->line($this->filament_notification->getBody())
->action($action ? $action->getLabel() : "Visit App", $action ? $action->getUrl() : env("APP_URL"));
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class FilamentNotificationEmail extends Notification
{
use Queueable;

protected \Filament\Notifications\Notification $filament_notification;

/**
* Create a new notification instance.
*/
public function __construct(\Filament\Notifications\Notification $filament_notification)
{
$this->filament_notification = $filament_notification;
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{

$action = $this->filament_notification->getActions();
$action = $action[0] ?? null;

return (new MailMessage)
->subject($this->filament_notification->getTitle())
->greeting('Hello ' . $notifiable->name . ',')
->line($this->filament_notification->getBody())
->action($action ? $action->getLabel() : "Visit App", $action ? $action->getUrl() : env("APP_URL"));
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
17 replies
FFilament
Created by Yiğit Kerem on 8/16/2024 in #❓┊help
Deliver notifications in email
Thank you so much!
17 replies
FFilament
Created by Yiğit Kerem on 8/16/2024 in #❓┊help
Deliver notifications in email
Hey, is there any way to accomplish this without having to change the namespaces? (Adding the modifier to the original Filament class) Not sure if that is possible in terms of how php itself works, obviously
17 replies
FFilament
Created by Yiğit Kerem on 8/16/2024 in #❓┊help
Deliver notifications in email
Thank you so much! Is it okay if I keep this discussion open while I try to implement it for any issues that may arise or should I reopen if any help is required?
17 replies
FFilament
Created by Yiğit Kerem on 8/16/2024 in #❓┊help
Deliver notifications in email
or does it need to be a plugin?
17 replies
FFilament
Created by Yiğit Kerem on 8/16/2024 in #❓┊help
Deliver notifications in email
Do I just create a new class than extends Filament\Notification?
17 replies