我的UserController中有这段代码。
public function update(Request $r, User $user)
{
$unEncryptedPassword = $r->password;
$encryptedPassword = bcrypt($unEncryptedPassword);
$r->password = $encryptedPassword;
$user->update($r->all());
return redirect('/users')->with('update', '');
}
我打算加密密码,然后将加密的密码插入数据库中。在$user->update($r->all());
之前,我检查了加密密码的回显,确实如此。问题是,当我提交该表单时,它会插入未加密的密码,我不知道为什么。
答案 0 :(得分:2)
尝试此操作,而不是修改请求:
public function update(Request $r, User $user)
{
$unEncryptedPassword = $r->password;
$encryptedPassword = bcrypt($unEncryptedPassword);
$requestData = $r->all();
$requestData['password'] = $encryptedPassword;
$user->update($requestData);
return redirect('/users')->with('update', '');
}
答案 1 :(得分:1)
尝试将此变体添加到用户模型中。
public function setPasswordAttribute($value)
{
$this->attributes['password'] = bcrypt($value);
}
然后将纯文本密码传递给用户模型。