一个简单的问题:在使用 Laravel Backpacker CRUD admin 保存请求值之前如何修改(散列)请求值?
据我所知,它应该在 crud Controller 中执行这些方法之前的某处完成:
public function store(StoreRequest $request)
{
return parent::storeCrud();
}
public function update(UpdateRequest $request)
{
return parent::updateCrud();
}
但我不知道如何正确地做到这一点。
编辑:请求不是 Request
对象,而是 StoreRequest
或 UpdateRequest
看起来像这个:
修复:
public function update(UpdateRequest $request)
{
// Hash password before save
if (!empty($request->password)) {
$request->offsetSet('password', Hash::make($request->password));
}
return parent::updateCrud($request); // <-- Pass the modified request, otherwise the CRUD reads it again from post data
}
最佳答案
您可以使用 offsetSet 更新 $request 值方法
$request->offsetSet('name', $newName);
编辑:要更新用户密码,您可以这样做:
public function update_password(Request $request)
{
$user = User::find(Auth::user()->id);
if (Hash::check($request->old_password, $user->password)) {
$user->fill([
'password' => Hash::make($request->password)
])->update();
return redirect()->back()->with('message' => 'Your password has been updated.');
}
else {
return redirect()->back()->with('message' => 'The password entered do not match our records.');
}
}
我没有检查代码,但它应该可以工作。现在根据您的需要对其进行更新。
关于php - 在使用 Laravel Backpacker 保存之前哈希密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41121167/