F
Filamentβ€’14mo ago
Quin.

Notification broadcasting with an observer on create

Hey! I have a question about the filament notifications, Right now i have when i edit my Opportunity that it sends a broadcast.
public function save(bool $shouldRedirect = true): void
{
$user = auth()->user();

$user->notify(
Notification::make()
->title('Saved successfully')
->toBroadcast(),
);
}
public function save(bool $shouldRedirect = true): void
{
$user = auth()->user();

$user->notify(
Notification::make()
->title('Saved successfully')
->toBroadcast(),
);
}
I really wanted that when i call my notification
class OpportunityNotification extends Notification implements ShouldQueue
{
use Queueable;

private Opportunity $opportunity;

public function __construct(Opportunity $opportunity)
{
//
$this->opportunity = $opportunity;
}

public function via($notifiable): array
{
return ['broadcast', 'slack'];
}

public function toBroadcast($notifiable): BroadcastMessage
{
return new BroadcastMessage([
'opportunity' => 'Checkout this new opportunity: '.$this->opportunity->title."\n".$this->opportunity->url,
]);
}

public function toSlack($notifiable): SlackMessage
{
return (new SlackMessage)
->content('Checkout this new opportunity: '.$this->opportunity->title."\n".$this->opportunity->url);
}

public function toArray($notifiable): array
{
return [];
}
}
class OpportunityNotification extends Notification implements ShouldQueue
{
use Queueable;

private Opportunity $opportunity;

public function __construct(Opportunity $opportunity)
{
//
$this->opportunity = $opportunity;
}

public function via($notifiable): array
{
return ['broadcast', 'slack'];
}

public function toBroadcast($notifiable): BroadcastMessage
{
return new BroadcastMessage([
'opportunity' => 'Checkout this new opportunity: '.$this->opportunity->title."\n".$this->opportunity->url,
]);
}

public function toSlack($notifiable): SlackMessage
{
return (new SlackMessage)
->content('Checkout this new opportunity: '.$this->opportunity->title."\n".$this->opportunity->url);
}

public function toArray($notifiable): array
{
return [];
}
}
That is called in my observer created
public function created(Opportunity $opportunity): void
{
$opportunity->notify(new OpportunityNotification($opportunity));
}
public function created(Opportunity $opportunity): void
{
$opportunity->notify(new OpportunityNotification($opportunity));
}
When i create this record that the notification will send in my laravel dashboard just as it does in mine save function for the edit notification? Is there anyway or better way to do this? Couldn't really find antything in the docs
Solution:
public function created(Opportunity $opportunity): void
{
$user = auth()->user();
$user->notify(Notification::make()->title('Checkout this new opportunity: '. $opportunity->title."\n". $opportunity->url)->toBroadcast());
}
public function created(Opportunity $opportunity): void
{
$user = auth()->user();
$user->notify(Notification::make()->title('Checkout this new opportunity: '. $opportunity->title."\n". $opportunity->url)->toBroadcast());
}
...
Jump to solution
1 Reply
Solution
Quin.
Quin.β€’14mo ago
public function created(Opportunity $opportunity): void
{
$user = auth()->user();
$user->notify(Notification::make()->title('Checkout this new opportunity: '. $opportunity->title."\n". $opportunity->url)->toBroadcast());
}
public function created(Opportunity $opportunity): void
{
$user = auth()->user();
$user->notify(Notification::make()->title('Checkout this new opportunity: '. $opportunity->title."\n". $opportunity->url)->toBroadcast());
}
i thought to difficult this is the easy way to do it πŸ˜„
Want results from more Discord servers?
Add your server