我正在设置页面上,用户在输入字段中看到其预填的用户名和电子邮件。 我喜欢检测(没有jQuery)用户是否更改了输入中的预填充值。
对于密码重置字段(未预填写),我使用以下逻辑:
if($request['password'] != ""){
if(!(Hash::check($request['password'], Auth::user()->password))){
return redirect()->back()->with('error', 'Your password does not match with the password you provided.');
}
if(strcmp($request['password'], $request['new_password']) == 0){
return redirect()->back()->with('error', 'New password cannot be same as your old one.');
}
$validation = $request->validate([
'password' => 'required',
'new_password' => 'required|string|min:6|confirmed',
]);
$user->password = bcrypt($request['new_password']);
$user->save();
return redirect()->back()->with('alert-success', 'Password changed
successfully');
}
我如何检查用户是否更改了预填充代码以使用以下过程:
if($request[''] ????? ""){
$user = Auth::user();
$user->name = $request['name'];
$user->email = $request['email'];
$user->save();
}
谢谢
答案 0 :(得分:2)
我认为,您不需要所有这些。只需使用以下内容。如果值更改,它只会真正更新模型。如果值没有更改(即,提交的数据与数据库中已存储的数据相同),则不会进行数据库查询。
$user = Auth::user();
$user->fill($request->only(['name', 'email']));
$user->save();
答案 1 :(得分:0)
经过长时间搜索,我发现isDirty()
检查数据库信息是否已更改,但是在发送之前我需要检查预填充的输入是否已更改,因此我添加了第二个操作按钮,现在使用switch语句获取更多信息。控制行为,例如:
public function profilePost(UserUpdate $request)
{
switch($request->input('action')){
case 'save_user':
$user = Auth::user();
$user->name = $request['name'];
$user->email = $request['email'];
$user->save();
return redirect()->back()->with('alert-success', 'Your profile information changed successfully');
break;
case 'save_password':
if($request['password'] != ""){
if(!(Hash::check($request['password'], Auth::user()->password))){
return redirect()->back()->with('error', 'Your current password does not match with the password you provided.');
}
if(strcmp($request['password'], $request['new_password']) == 0){
return redirect()->back()->with('error', 'New password cannot be same as your current one.');
}
$validation = $request->validate([
'password' => 'required',
'new_password' => 'required|string|min:6|confirmed'
]);
$user = Auth::user();
$user->password = bcrypt($request['new_password']);
$user->save();
return redirect()->back()->with('alert-success', 'Password changed
successfully');
} else {
return redirect()->back()->with('error', 'No Password was changed');
}
break;
}
}