更改 Laravel 作业 Class 中的参数值
Change value of parameters inside Laravel Job Class
我正在尝试发送一封电子邮件,其中包含 class 在 Laravel 的工作,关键是我需要跟踪通过邮件发送的通知。我有一个名为 "Notifications" 的 table,其中包含列 status 和 lastUpdated(这些是关于此问题的列)。
为了更新通知行 table 我需要知道该行的 ID。
我正在尝试在作业 class 中更改作为参数发送的模型的参数值。这个模型是"Notifications"
我的问题是,作业 class 中的参数一旦从 class 中更改后是否会持续到下一次调用?显然不是那样工作的。如果这不可能,有人可以提出任何解决方法吗?
class SendReminderEmail extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function __construct($lastNotification)
{
$this->lastNotification = $lastNotification;
}
//NULL means it's the first time the job has been called
protected $lastNotification;
public function handle(Mailer $mailer)
{
$mailer->send('user.welcome', ['username' => 'Luis Manuel'], function($message)
{
$message->from('mymail@mymail.com', 'Luis');
$message->to('mymail@mymail.com', 'mymail@mymail.com');
$message->subject('Subject here!');
});
if(count($mailer->failures()) > 0)
{
if($this->lastNotification == null)
{
$this->lastNotification = new Notification;
$this->lastNotification->status = false;
$this->lastNotification->lastUpdated = Carbon::now();
}
else
{
$this->lastNotification->status = false;
$this->lastNotification->lastUpdated = Carbon::now();
}
$this->lastUpdated->save();
}
}
}
我是怎么解决的:
在控制器内调度作业之前,我创建了 Notification 模型,将其保存,然后将其作为参数传递给作业 class。
public function ActionSomeController()
{
$notification = new Notifiaction;
$notification->save();
$newJob = (new SendReminderEmail($notification));
$this->dispatch($newJob);
}
在工作中 Class :
public function __construct($notification)
{
$this->notification = notification;
}
public function handler(Mailer $mailer)
{
/*
* Some extra code abover here
*/
$this->notification->lastUpdated = Carbon::now();
}
它就像一个魅力!
我正在尝试发送一封电子邮件,其中包含 class 在 Laravel 的工作,关键是我需要跟踪通过邮件发送的通知。我有一个名为 "Notifications" 的 table,其中包含列 status 和 lastUpdated(这些是关于此问题的列)。
为了更新通知行 table 我需要知道该行的 ID。 我正在尝试在作业 class 中更改作为参数发送的模型的参数值。这个模型是"Notifications"
我的问题是,作业 class 中的参数一旦从 class 中更改后是否会持续到下一次调用?显然不是那样工作的。如果这不可能,有人可以提出任何解决方法吗?
class SendReminderEmail extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function __construct($lastNotification)
{
$this->lastNotification = $lastNotification;
}
//NULL means it's the first time the job has been called
protected $lastNotification;
public function handle(Mailer $mailer)
{
$mailer->send('user.welcome', ['username' => 'Luis Manuel'], function($message)
{
$message->from('mymail@mymail.com', 'Luis');
$message->to('mymail@mymail.com', 'mymail@mymail.com');
$message->subject('Subject here!');
});
if(count($mailer->failures()) > 0)
{
if($this->lastNotification == null)
{
$this->lastNotification = new Notification;
$this->lastNotification->status = false;
$this->lastNotification->lastUpdated = Carbon::now();
}
else
{
$this->lastNotification->status = false;
$this->lastNotification->lastUpdated = Carbon::now();
}
$this->lastUpdated->save();
}
}
}
我是怎么解决的:
在控制器内调度作业之前,我创建了 Notification 模型,将其保存,然后将其作为参数传递给作业 class。
public function ActionSomeController()
{
$notification = new Notifiaction;
$notification->save();
$newJob = (new SendReminderEmail($notification));
$this->dispatch($newJob);
}
在工作中 Class :
public function __construct($notification)
{
$this->notification = notification;
}
public function handler(Mailer $mailer)
{
/*
* Some extra code abover here
*/
$this->notification->lastUpdated = Carbon::now();
}
它就像一个魅力!