$ request变量未定义

时间:2016-11-24 09:31:50

标签: php laravel

在我的store方法中,我使用$ request请求,但是当传递给显示未定义变量请求错误的Mail::send方法时。

public function store(Request $request)
    {
        $user = new User();
        $user->add_user($request);
        $confirmation_code = ['123456'];
        Mail::send('users.add-user', $confirmation_code, function($message) {
            $message->to($request->get('email'), $request->get('email'))
                ->subject('Verify your email address');
        });
        return redirect('/users')->with('success', ['Thanks for signing up! Please check your email.']); ;
    }

3 个答案:

答案 0 :(得分:2)

在你的Mail :: send闭包中,你必须传递$request变量,如下所示:

Mail::send('users.add-user', $confirmation_code, function($message) use ($request) {
    $message->to($request->get('email'), $request->get('email'))
        ->subject('Verify your email address');
});

答案 1 :(得分:1)

请尝试此代码..

public function store(Request $request)
{
    $user = new User();
    $user->add_user($request);
    $confirmation_code = ['123456'];
    Mail::send('users.add-user', $confirmation_code, function($message) use($request) {
        $message->to($request->email, $request->email)
                ->subject('Verify your email address');
    });
    return redirect('/users')->with('success', ['Thanks for signing up! Please check your email.']); ;
}

答案 2 :(得分:0)

您应该添加use()以将变量传递到闭包范围:

Mail::send('users.add-user', $confirmation_code, function($message) use($request) {