Php 检测laravel输入预填充值的变化
我在一个设置页面上工作,如果用户在输入字段中看到其预填充的用户名和电子邮件。Php 检测laravel输入预填充值的变化,php,validation,laravel-5,Php,Validation,Laravel 5,我在一个设置页面上工作,如果用户在输入字段中看到其预填充的用户名和电子邮件。 我喜欢检测(在我看来,没有jQuery的情况下,您不需要所有这些。只需使用以下方法。只有当值发生更改时,它才会真正更新模型。如果值没有更改(即提交的数据与数据库中已存储的数据相同),则不会进行数据库查询 $user = Auth::user(); $user->fill($request->only(['name', 'email'])); $user->save(); 在我看来,您不需要所有这些。
我喜欢检测(在我看来,没有jQuery的情况下,您不需要所有这些。只需使用以下方法。只有当值发生更改时,它才会真正更新模型。如果值没有更改(即提交的数据与数据库中已存储的数据相同),则不会进行数据库查询
$user = Auth::user();
$user->fill($request->only(['name', 'email']));
$user->save();
在我看来,您不需要所有这些。只需使用以下方法。只有当值发生更改时,它才会真正更新模型。如果值没有更改(即提交的数据与数据库中已存储的数据相同),则不会进行数据库查询
$user = Auth::user();
$user->fill($request->only(['name', 'email']));
$user->save();
经过长时间的搜索,我找到了
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;
}
}
经过长时间的搜索,我找到了
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;
}
}