如何在Laravel中向通知markdown传递数据?

3

我正在尝试将变量传递到我的通知标记语言中(而不是直接通过通知的toMail方法),因为我需要多个操作按钮。我希望用户能够回答电子邮件中提出的问题。以下是我在通知标记语言中尝试的内容:

@component('mail::button', ['url' => 'https://myapp.dev/response/' . $event->id . '/' . $user->id . '/yes'])
Yes
@endcomponent

@component('mail::button', ['url' => 'https://myapp.dev/response/' . $event->id . '/' . $user->id . '/no'])
No
@endcomponent

以下是通知本身的代码:

class eventAlert extends Notification
{
use Queueable;

public $event;
public $user;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct(Event $event, User $user)
{
    $this->event = $event;
    $this->user = $user;
}

public function toMail($notifiable)
{

    return (new MailMessage)
                ->greeting('Hey ' . $this->user->fn)
                ->line('There is an event today @ '. $this->event->edatetime->format('h:i A'))
                ->line($this->event->location)
                ->markdown('mail.notifications.eventalert');
}

但是,我尝试运行计划任务时遇到一个错误,即事件变量未定义。我知道对于Mailable类,您可以在标记中使用通过构造函数传递的数据,因此我尝试将我的变量从protected更改为public,就像Mailable类所需的那样,但这仍然不起作用。如何将我的变量数据传递到通知标记中?


你的应用程序中通知发送到哪里了?在实例化通知时,是否传递了 $event 对象? - Chinonso Chukwuogor
我通过一个“Job”发送它。它会检查今天是否有事件,如果有,就会通知所有参与该事件的用户。一切都很顺利,直到我尝试将像$event->id这样的数据添加到markdown中。是的,在Job的handle方法中,$user->notify(new eventAlert($event, $user)); - develpr
1个回答

2
如果您在函数中将其分配给一个变量,会发生什么情况,例如: ``` 如果您在函数中将其分配给一个变量,会发生什么情况? ```
public function toMail($notifiable)
{
    $aux = $this->event;

    return (new MailMessage)
                ->greeting('Hey ' . $this->user->fn)
                ->line('There is an event today @ '. $aux->edatetime->format('h:i A'))
                ->line($aux->location)
                ->markdown('mail.notifications.eventalert');
}

如果这个方法仍然不起作用,可能是事件没有被正确地分配。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接