我试图通过获取旧密码更改密码值它工作正常但我的问题是当我尝试更新我的字段时将新密码值保存为空(因为我将此字段留空)我想要更新仅当我更改新密码值时才使用新密码,否则它会在新密码字段中保留当前密码
这里是我的控制器
public function profileupdate(Request $request,$id)
{
if(Auth::Check())
{
$request_data = $request->All();
$validator = $this->validator($request_data);
if($validator->fails())
{
$this->throwValidationException($request, $validator);
}
else
{
$current_password = Auth::User()->password;
if(Hash::check($request_data['current-password'], $current_password))
{
$user_id = Auth::User()->id;
$admin = Admin::find($user_id);
$admin->update([
'name'=>$request['name'],
'job_title'=> $request['job_title'],
'email'=>$request['email'],
'phone_number'=>$request['phone_number'],
'password'=> Hash::make($request['password']),
]);
return redirect('/admin/profile')
->with('message', 'Admin Profile updated successfuly!');
}
else
{
return redirect('/admin/profile')
->with('password', 'Please enter correct password!');
}
}
}
else
{
return redirect()->to('/');
}
}
在我的观点中
<div class="control-group{{ $errors->has('current-password') ? ' has-error' : '' }}">
<label class="control-label"> Current Password:</label>
<div class="controls">
<input type="password" class="form-control" id="current-password" name="current-password" placeholder="Password">
@if(Session::has('password')) <span class="help-inline"> <strong>{{Session::get('password')}} </strong></span> @endif
@if ($errors->has('current-password'))
<span class="help-inline">
<strong>{{ $errors->first('current-password') }}</strong>
</span>
@endif
</div>
</div>
<div class="control-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="control-label"> New Password:</label>
<div class="controls">
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
@if ($errors->has('password'))
<span class="help-inline">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="control-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label class="control-label">Confirm Password:</label>
<div class="controls">
<input type="password" class="form-control" id="password_confirmation" name="password_confirmation" placeholder="Re-enter Password">
@if ($errors->has('password_confirmation'))
<span class="help-inline">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
@endif
</div>
</div>
答案 0 :(得分:0)
只需构建一个动态更新值而不是静态更新值的数组。大多数数组仍然是静态的,但现在动态添加密码,具体取决于传递给表单中password
字段的值。
// Values which are always updated
$update_values = [
'name'=>$request['name'],
'job_title'=> $request['job_title'],
'email'=>$request['email'],
'phone_number'=>$request['phone_number']
];
// If the password-field isn't empty, we add it to the values that are updated
if (!empty($request['password']))
$update_values['password'] = Hash::make($request['password']);
// Execute the update
$admin->update($update_values);