2016-11-04 54 views
2

我正在編輯配置文件,並且我希望密碼不應該更新,如果用戶沒有給出密碼字段中的任何值現在如果用戶沒有輸入任何值然後密碼自動更新如果用戶在密碼字段laravel中沒有給出任何值,則密碼不應該更新5.3

這裏是我的方法

{ 
$this->validate($request, [ 
      'name' => 'required|max:255', 

     ]); 

     $user = User::findOrFail($id); 
     $input = $request->all(); 
     $user->fill([ 
     'name'   => $request->input('name'), 
     'password'  => bcrypt($request->input('password')), 
     'def_timezone' => $request->input('def_timezone'), 
     'address_line_1' => $request->input('address_line_1'), 
     ])->save(); 

    session()->flash('msg',trans('successfully Updated.')); 
    } 

請幫忙解決這個問題由於

回答

5

你可以這樣做:

'password' => $request->password ? bcrypt($request->password) : $user->password, 

此外,您可以使用update()方法而不是fill()save()。採用這種方法的空值將被忽略:

$user->update($request->all()); 
+1

感謝其工作@Alexey Mezenin –

0

你必須做這樣的事情到您的編輯驗證

'password' => 'min:6|max:50', 

所以用戶傾斜發送一個空密碼到您的分貝:

如果你想在你更新用戶密碼之前檢查,最好在你的視圖中有另一個字段爲Old_password,這樣你就可以檢查用戶是否記得舊密碼並且可以將它更改爲新密碼。 這裏是我的例子我的更新功能:

public function update(Request $request, $id) 
{ 


    $update_user = Validator::make($request->all(), [ 
     'name' => 'min:2|max:35|string', 
     'surname' => 'min:2|max:35|string',    
     'email' => Sentinel::inRole('Admin')?'required|email|min:3|max:50|string':(Sentinel::check()?'required|email|min:3|max:50|string|unique:users,email,'.$id:'required|email|min:3|max:50|unique:users|string'), 
     'old_password' => 'min:6|max:50', 
     'new_password' => 'min:6|max:50', 
    ]); 

    if ($update_user->fails()) { 
     return redirect()->back() 
        ->withErrors($update_user) 
        ->withInput(); 
    } 

    $user = User::find($id); 
    if ($user) { 

     if(!empty($request->file('image'))){ 
       $file = $request->file('image'); 
       $destinationPath = public_path() . '/upload-client/profile_image'; 
       $filename = str_random(6) . '_' . $file->getClientOriginalName(); 
       $unwanted = array("\'", "+", "%"); 
       $filename =str_replace($unwanted, "", $filename); 

       $uploadSuccess = $file->move($destinationPath, $filename); 
       $user->image_path=$filename; 
      } 
      if($request->name){ 
      $user->name=$request->name; 
      } 
      if($request->email){ 
      $user->email=$request->email; 
      } 
      if($request->old_password){ 
      if (Hash::check($request->old_password, $user->password)){ 
       $user->password=bcrypt($request->new_password); 
      }else{ 
       Session::flash('message', 'Your old password is incorrect.'); 
       Session::flash('status', 'error'); 
       return redirect()->back()->withErrors(['old_password', 'your old password is incorrect']); 
      } 
      } 

     $user->update(); 
     if ($request->role) { 
      $user->roles()->sync([$request->role]); 
     } 
     Session::flash('message', 'Success! User is updated successfully.'); 
     Session::flash('status', 'success'); 

    } 


    return redirect()->back(); 
} 
相關問題