foreach循环不与laravel队列一起工作

时间:2015-06-17 13:08:45

标签: php laravel-4

我使用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

}

我在这里做错了什么?为什么它不像普通循环那样工作?

1 个答案:

答案 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']
    ]);

问题解决了