【发布时间】:2020-11-13 21:02:36
【问题描述】:
我在 Laravel 中遇到了队列问题,因为我以前从未使用过它们。我在 toMailUsing 方法和专门的服务提供商的帮助下覆盖了默认的重置密码电子邮件:
class MailServiceProvider extends ServiceProvider
{
public function boot()
{
ResetPassword::toMailUsing(function ($notifiable, $token) {
$url = url(route('password.reset', ['token' => $token, 'email' => $notifiable->getEmailForPasswordReset()]));
dispatch(new SendEmail($url, $notifiable));
});
}
}
这是我的SendEmail 工作类:
class SendEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
protected $url;
public function __construct($url, $user)
{
$this->user = $user;
$this->url = $url;
}
public function handle()
{
$email = new ResetPassword($this->url, $this->user);
Mail::to($this->user->email)->send($email);
}
}
还有邮件本身:
class ResetPassword extends Mailable
{
use Queueable, SerializesModels;
protected $url;
protected $user;
public function __construct($url, $user)
{
$this->url = $url;
$this->user = $user;
}
public function build()
{
return $this->markdown('emails.password_reset', ['url' => $this->url, 'user' => $this->user]);
}
}
问题出在哪里?我成功排队作业并收到电子邮件,但收到错误:
Trying to get property 'view' of non-object
堆栈跟踪:https://flareapp.io/share/87nOGYM5#F59
这是我以前的工作代码:
//Provider
ResetPassword::toMailUsing(function ($notifiable, $token) {
$url = url(route('password.reset', ['token' => $token, 'email' => $notifiable->getEmailForPasswordReset()]));
return new ResetPasswordMail($url, $notifiable);
});
//Mailable
class ResetPassword extends Mailable
{
use Queueable, SerializesModels;
protected $url;
protected $user;
public function __construct($url, $user)
{
$this->url = $url;
$this->user = $user;
}
public function build()
{
$address = 'noreply@' . config('app.domain');
$name = 'Lorem ipsum';
$subject = config('app.name') . ' - Próba zresetowania hasła';
$this->to($this->user)->subject($subject)->from($address, $name)->markdown('emails.password_reset', ['url' => $this->url, 'user' => $this->user]);
}
}
非常感谢任何帮助。
【问题讨论】:
-
错误信息告诉你问题出在哪里。您应该将其包含在您的问题中。
-
toMailUsing不需要返回MailMessage通知的实例吗?你也不会从闭包中分派工作。
标签: php laravel laravel-queue laravel-mail