FilamentF
Filamentβ€’3y ago
Quin.

Sending a notification with an observer

Ello!
I was reading the documentation but i couldn't find the right answer on my question. I wanted to send a notification when Opportunity created? Is there anyway to do this in the observer that it sends the notification on your laravel dashboard?

OpportunityNotification:

  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 [];
    }


OpportunityObserver:
   public function created(Opportunity $opportunity): void
    {
        $opportunity->notify(new OpportunityNotification($opportunity));
    }


i am broadcasting the notification πŸ˜„
Was this page helpful?