在 Laravel 5.4 中获得 "Undefined variable: emails"
Getting "Undefined variable: emails" in Laravel 5.4
为什么我在 Laravel 5.4 中得到 "Undefined variable: emails",但相同的代码在 Laravel 5.2 中运行良好?下面是一段代码。不要认为这很重要,但我正在使用 PHP 7.1.3.
public function send(Request $request) {
$emails = "";
try {
$emails = [$request->input('to'), "john.doe@gmail.com"];
Mail::send('email.contact', ['request' => $request], function($message) use ($request) {
$message->from($request->input('email'), $request->input('email'));
$message->to($emails[0], $emails[0])
->cc($emails[1], $emails[1])
->subject("Contact Us");
});
$response = array (
'success' => true,
'message' => 'Message sent.',
$request
);
}
catch(Exception $e) {
$response = array (
'success' => false,
'message' => $e->getMessage(),
$request
);
}
// return Response::json( $response );
return $response;
}
只需在匿名函数中使用 $emails
变量和 $request
变量即可。
试一试的完整代码。
public function send(Request $request) {
$emails = "";
try {
$emails = [$request->input('to'), "john.doe@gmail.com"];
Mail::send('email.contact', ['request' => $request], function($message) use ($request, $emails) {
# Here add $emails variable in use list.
$message->from($request->input('email'), $request->input('email'));
$message->to($emails[0], $emails[0])
->cc($emails[1], $emails[1])
->subject("Contact Us");
});
$response = array (
'success' => true,
'message' => 'Message sent.',
$request
);
}
catch(Exception $e) {
$response = array (
'success' => false,
'message' => $e->getMessage(),
$request
);
}
// return Response::json( $response );
return $response;
}
希望对您有所帮助。
问题在这里:
use ($request) {
要在匿名函数中使用 $emails
,您必须将其传递到此处,例如:
use ($request, $emails) {
现在可以使用了。
为什么我在 Laravel 5.4 中得到 "Undefined variable: emails",但相同的代码在 Laravel 5.2 中运行良好?下面是一段代码。不要认为这很重要,但我正在使用 PHP 7.1.3.
public function send(Request $request) {
$emails = "";
try {
$emails = [$request->input('to'), "john.doe@gmail.com"];
Mail::send('email.contact', ['request' => $request], function($message) use ($request) {
$message->from($request->input('email'), $request->input('email'));
$message->to($emails[0], $emails[0])
->cc($emails[1], $emails[1])
->subject("Contact Us");
});
$response = array (
'success' => true,
'message' => 'Message sent.',
$request
);
}
catch(Exception $e) {
$response = array (
'success' => false,
'message' => $e->getMessage(),
$request
);
}
// return Response::json( $response );
return $response;
}
只需在匿名函数中使用 $emails
变量和 $request
变量即可。
试一试的完整代码。
public function send(Request $request) {
$emails = "";
try {
$emails = [$request->input('to'), "john.doe@gmail.com"];
Mail::send('email.contact', ['request' => $request], function($message) use ($request, $emails) {
# Here add $emails variable in use list.
$message->from($request->input('email'), $request->input('email'));
$message->to($emails[0], $emails[0])
->cc($emails[1], $emails[1])
->subject("Contact Us");
});
$response = array (
'success' => true,
'message' => 'Message sent.',
$request
);
}
catch(Exception $e) {
$response = array (
'success' => false,
'message' => $e->getMessage(),
$request
);
}
// return Response::json( $response );
return $response;
}
希望对您有所帮助。
问题在这里:
use ($request) {
要在匿名函数中使用 $emails
,您必须将其传递到此处,例如:
use ($request, $emails) {
现在可以使用了。