在laravel中,foreach变量未定义

时间:2015-05-27 05:34:01

标签: variables foreach undefined

我在laravel中收到了$ recipient变量未定义的错误。

public function sendEmail($input, $recipients){

    foreach($recipients as $recipient){
            $student = StudentInfo::where('email', '=', $recipient)->first(); 
            Mail::queue('emails.outMail', array('letter' => $input['dept_message']), function($message){
                $message->to($recipient, $student->first_name)->subject($input['dept_subject']);
            });
        }

    }

1 个答案:

答案 0 :(得分:-1)

在闭包功能中,您需要使用$ recipient。

    function($message) use ($recipient){

         //code

    }

更新代码

public function sendEmail($input, $recipients){
    foreach($recipients as $recipient){
            $student = StudentInfo::where('email', '=', $recipient)->first(); 
            Mail::queue('emails.outMail', array('letter' => $input['dept_message']), function($message) use ($recipient) {
                $message->to($recipient, $student->first_name)->subject($input['dept_subject']);
            });
        }
    }