我使用sync
(本地驱动程序)在update
EmailCampaignController
方法中推送队列,该方法使用名为emailQueue
<的同一控制器的另一种方法/ p>
像这样
Queue::push('EmailNewsletterController@emailQueue', array('campaign_id' => $campaign_id));
emailQueue
使用foreach
循环,该循环正确运行一次,之后就会出现错误,好像$campaign_id
未定义
这是emailQueue
方法
public function emailQueue($job, $data) {
// Queue Starts here
$emailCampaign = EmailCampaign::find($data['campaign_id']);
$emailCampaign->status = 'In Progress';
$emailCampaign->last_activity = Carbon::now();
$emailCampaign->save();
$data = $emailCampaign->emailCampaignNewsletter;
$contacts = $emailCampaign->contactList->contacts;
foreach ($contacts as $contact) {
$emailBody = [
'message' => [
'subject' => $data['email_title'],
'html' => $data['email_body'],
'from_email' => $data['from_email'],
'to' => [['email' => $contact['email_address']]]
]
];
$response = Mandrill::request('messages/send', $emailBody);
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
$contact->last_activity = Carbon::now();
$contact->save();
}
$emailCampaign->status = 'Sent';
$emailCampaign->save();
$job->delete();
// Ends here
}
我在这里做错了什么?为什么它不像普通循环那样工作?
答案 0 :(得分:0)
问题是email_campaign_id
为空,因为$data['campaign_id']
为空,正确的外键是$data['email_campaign_id']
是什么阻止了进程 - 我应该在放入之前测试它它在队列中
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
到
EmailCampaignRecord::create([
'email_campaign_id' => $data['email_campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
问题解决了