Search code examples
phplaravellaravel-4campaign-monitor

Add subscriber to multiple lists in Campaign Monitor


I'm trying to add a subscriber to multiple lists using a foreach loop. But the subscriber is only being added to 1 list.

So $course is sending the correct value every time. But the find method on TrainingCourse doesn't change the campaign monitor id according to what it should be. It's fetching the id for the wrong record.

Where am I going wrong?

foreach($selectedCourses as $course)
{
    $courseCMId = TrainingCourse::find($course)->first();

    $wrap = new CS_REST_Subscribers($courseCMId->campaign_monitor_id, $auth);

    $result = $wrap->add([
        'EmailAddress' => $registration->email,
        'Name'         => $registration->name,
        'Resubscribe'  => true
    ]);

    if(!$result->was_successful())
    {
        $cm['status_code'] = $result->http_status_code;
        return Redirect::to('training/register', compact('cm'))
                       ->withErrors($validator)
                       ->withInput(Input::except('password', 'password_confirmation'));
    }
}

Solution

  • Changing the line:

    $courseCMId = TrainingCourse::find($course)->first();
    

    to:

    $courseCMId = DB::table('training_courses')->where('id', $course)->first();
    

    Solved my problem