【发布时间】:2020-08-03 16:29:49
【问题描述】:
我的应用流程如下所示:
- 我创建了一张发票,发票状态=
created - 我将其发送给用户,我使用 Queueable,因此发送电子邮件的过程与主应用程序分开。
- 发送电子邮件完成后,发票状态应为
sent
问题是,对于 app/Mail 中的特定 Mailable 类,我找不到像 AfterEmailSent 这样的任何事件。
【问题讨论】:
我的应用流程如下所示:
created
sent
问题是,对于 app/Mail 中的特定 Mailable 类,我找不到像 AfterEmailSent 这样的任何事件。
【问题讨论】:
您需要创建自己的事件,首先创建一个:
php artisan make:event InvoiceEmailSentEvent
在Events\InvoiceEmailSentEvent.php 内添加以下内容:
public $invoice;
public function __construct($invoice)
{
$this->invoice = $invoice;
}
为事件创建一个监听器:
php artisan make:listener InvoiceEmailSentListener
对于Listeners\InvoiceEmailSentListener.php内的handle()函数,添加
//import your Mailable up here
public function handle(InvoiceEmailSentEvent $event)
{
// You don't have to sent email here, but I just added it if $invoice contains an email field
$email = $event->invoice->email;
Mail::to($email)->send(new Your_Mailable_Class_Goes_Here($event->invoice));
// UPDATE INVOICE STATUS HERE
}
在App\Providers\EventServiceProvider.php 的$listen 数组中注册您的事件和侦听器
'App\Events\InvoiceEmailSentEvent' => [
'App\Listeners\InvoiceEmailSentListener',
],
最后,在您的控制器中,您可以调用事件,传递您要更新的发票,例如
$invoice = Invoice::findOrFail($invoice_id)->first();
event(new InvoiceEmailSentEvent($invoice));
【讨论】: