laravel变量未定义

时间:2014-04-25 09:37:02

标签: php laravel laravel-4

这很奇怪。我确信我错过了一些简单的事情。我有以下代码:

$productToken = Input::get('key');
        $products = new Product;
        $userEmail = $products->activateProduct($productToken);

        $productDetailsArray = $products->getProductDetailsForUserEmail($productToken);

        $emailData = $productDetailsArray;
        Mail::send('emails.productReleased', $emailData, function($message){
            $message->to($userEmail)->subject('Notification - Your Product was Released to the Public!');
        }); 

应该在数据库中激活产品,然后向用户发送电子邮件。用户的电子邮件位于$ userEmail中,当我执行var_dump时,它会显示。不知怎的,这一行引发了$ userEmail未定义的错误:

$message->to($userEmail)->subject('Notification - Your Product was Released to the Public!');

这是我得到的错误:

Undefined variable: userEmail

之前我使用过邮件功能,但不是传递变量而是传递了Input :: get(' email'),因为它是在注册表单中。现在,我无法访问输入,而是$ userEmail。请指教。

2 个答案:

答案 0 :(得分:4)

您正在使用回调函数,因此在调用函数时,userEmail变量肯定超出了范围。您应该将userEmail变量发送到该函数,可能是这样的:

Mail::send('emails.productReleased', $emailData, function($message) use ($userEmail) {
    $message->to($userEmail)->subject('Notification - Your Product was Released to the Public!');
}); 

有关lambda(匿名)函数和上下文的信息,请参阅http://www.php.net/manual/fr/functions.anonymous.php#example-191

答案 1 :(得分:0)

您必须使用$userEmailuse变量传递到结束范围。

function($message) use ($userEmail)

有关使用匿名函数的更多信息,请查看here

相关问题