product = $product; $this->user = $user; $this->action = $action; } /** * Get the notification's delivery channels. * * @return array */ public function via(object $notifiable): array { $channels = []; $prefs = $notifiable->preferences ?? []; // If admin wants to receive product change notifications if ($prefs['notifications_product_actions'] ?? true) { if ($prefs['notifications_email'] ?? true) { $channels[] = 'mail'; } if ($prefs['notifications_system'] ?? true) { $channels[] = 'database'; } } return $channels; } /** * Get the mail representation of the notification. */ public function toMail(object $notifiable): MailMessage { $actionText = $this->action === 'created' ? 'criado' : 'atualizado'; return (new MailMessage) ->subject('Produto ' . ucfirst($actionText) . ' - ' . config('app.name')) ->greeting('Olá, ' . $notifiable->name) ->line('Um produto do site foi ' . $actionText . ' por ' . $this->user->name . '.') ->line('**Nome do Produto:** ' . $this->product->name) ->line('**Preço:** R$ ' . number_format($this->product->price, 2, ',', '.')) ->action('Gerenciar Produtos', route('admin.products.index')) ->line('Obrigado por usar nosso sistema!'); } /** * Get the array representation of the notification. * * @return array */ public function toArray(object $notifiable): array { $actionText = $this->action === 'created' ? 'criado' : 'atualizado'; return [ 'product_id' => $this->product->id, 'name' => $this->user->name, 'email' => $this->user->email, 'action' => $this->action, 'message' => "Produto '{$this->product->name}' foi {$actionText}.", 'link' => route('admin.products.edit', $this->product->id) ]; } }