如果用户未在密码字段laravel 5.3中给出任何值,则不应更新密码

时间:2016-11-04 07:40:30

标签: php laravel

我正在编辑个人资料,如果用户没有在密码字段中给出任何值,如果用户没有输入任何值,则密码不会自动更新,我希望密码不会更新

这是我的方法

    {
$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.'));
    }

请帮助解决问题谢谢

2 个答案:

答案 0 :(得分:5)

你可以这样做:

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

此外,您可以使用update()方法代替fill()save()。使用此方法,将忽略空值:

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

答案 1 :(得分: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();
}