2017-04-19 16 views
0

我想通過獲取舊密碼更改密碼值它工作正常但我的問題是當我嘗試更新我的領域它保存新密碼值爲空(因爲我離開此字段爲空)我想更新新密碼,只有當我更改新的密碼值,否則它保持新密碼字段中的當前密碼更新密碼值只有當我更改密碼,否則它保留相同的值

這裏面我的控制器

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

哪裏有驗證規則?它不應該通過,如果它是空的,也可以嘗試'var_dump'請求來查看字段及其值。 – Gntem

回答

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);