Laravel 5.2:密码重置用户名的电子邮件

时间:2016-05-27 10:13:05

标签: php laravel laravel-5.2

我的情况是,用户的唯一电子邮件可以包含多个帐户。 表示一封电子邮件与唯一用户名的许多帐户相关联。

我目前的代码适用于邮件,我想用他们的用户名发送电子邮件,意味着用户将输入他的用户名,然后与该用户名关联的电子邮件将获得密码重置链接

直接电子邮件的当前工作代码:

public function postEmail(Request $request)
{
    $validator = Validator::make(Input::get(),
        [
            'email' => 'required|email'
        ]
    );
    if ($validator->fails()) {
        return redirect()
            ->back()
            ->withErrors($validator->errors())
            ->with('message', 'Please fix your form fields.')
            ->with('form', 'recover')
            ->withInput(Input::except('password'));
    }
    $response = $this->passwords->sendResetLink($request->only('email'), function($message)
    {
        $message->subject('Password Reminder');
    });
    switch ($response)
    {
        case PasswordBroker::RESET_LINK_SENT:
            return redirect()
                ->back()
                ->with('message', 'A verification email was sent to your email inbox.')
                ->with('form', 'recover')
                ->withInput(Input::except('password'));

        case PasswordBroker::INVALID_USER:
            dd('true');
    }
}

我添加了以下行:

$usernameToEmail = App\User::where('name','=', Input::get());

然后我将$usernameToEmail->email传递给

$response = $this->passwords->sendResetLink($usernameToEmail->email, 

    function($message)
       {
          $message->subject('Password Reminder');
       });

会引发以下错误:

Type error: Argument 1 passed to Illuminate\Auth\Passwords\PasswordBroker::sendResetLink() must be of the type array, string given

2 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为PasswordBroker正在尝试通过您提供的凭据检索使用情况,并且它将进行数据库查询以获取用户。因此,如果您的用户名是唯一的,您可以这样做:

$this->passwords->sendResetLink(
    ['username' => $username], 
    function($message){...}
);

答案 1 :(得分:0)

  

这不起作用,您需要拥有唯一的电子邮件和用户名。

因为令牌是针对表格中的唯一用户行生成的,并在重置时使用。