未定义的变量 - laravel 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37967384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:03:21  来源:igfitidea点击:

Undefined variable - laravel emailing

phplaravelemail

提问by Nicolas

I'm trying to send an email using laravel. This worked for serveral functions but with this one I'm getting a weird error.

我正在尝试使用 laravel 发送电子邮件。这适用于多个功能,但使用这个功能我遇到了一个奇怪的错误。

here's the code I'm using

这是我正在使用的代码

    $offerte = DB::table('offertes')
        ->select('*')
        ->where('id', '=', $offerte_id)
        ->get();

    Mail::send('emails.offerte_reactie', ['offerte' => $offerte, 'user' => $user, 'message_text' => $message_text], function ($message) use ($user)
    {
        $message->from($user->email, 'Foodtruckbestellen.be');
        $message->to($offerte->email);
        $message->subject('Reactie offerte Foodtruckbestellen.be');
    });

However when I want to send the mail I get the error

但是,当我想发送邮件时,出现错误

Undefined variable: offerte

未定义变量:offerte

The line $message->to($offerte->email);is indeed underlined in red in PHPSTORM and I don't use the variable in the email. so I know for sure that's where my error is I just can't find a fix for it.

$message->to($offerte->email); 在 PHPSTORM 中确实用红色下划线表示,我没有在电子邮件中使用该变量。所以我确定这就是我的错误所在,我只是找不到解决方法。

回答by Burak

You need to pass the $offertevariable to closure as it's not defined within the closure scope.

您需要将$offerte变量传递给闭包,因为它没有在闭包范围内定义。

Just use the $offertevariable as you do with the $user.

只需使用$offerte变量作为你与做$user

Mail::send('emails.offerte_reactie', ['offerte' => $offerte, 'user' => $user, 'message_text' => $message_text], function ($message) use ($user, $offerte)
{
    $message->from($user->email, 'Foodtruckbestellen.be');
    $message->to($offerte->email);
    $message->subject('Reactie offerte Foodtruckbestellen.be');
});

Furthermore, if I were you, I'd use compactfor the array that you pass to the send function's second argument to write more clean code.

此外,如果我是你,我会使用compact你传递给 send 函数的第二个参数的数组来编写更干净的代码。

Mail::send('emails.offerte_reactie', compact('offerte', 'user', 'message_text'), function ($message) use ($user, $offerte)
{
    $message->from($user->email, 'Foodtruckbestellen.be');
    $message->to($offerte->email);
    $message->subject('Reactie offerte Foodtruckbestellen.be');
});

回答by Mohamed Laabid

You shoud add variable $offerte to use function like that :

你应该添加变量 $offerte 来使用这样的函数:

    Mail::send('emails.offerte_reactie', ['offerte' => $offerte, 'user' => $user, 'message_text' => $message_text], function ($message) use ($user,$offerte)

回答by Hiren Makwana

You forgot to write $offertevariable hence.

你忘了写$offerte变量因此。

Mail::send('emails.offerte_reactie', ['offerte' => $offerte, 'user' => $user, 'message_text' => $message_text], function ($message) use ($user, $offerte)