laravel 5.2-检查字段是否为空

use*_*101 3 passwords field laravel

我想检查某个字段是否为空。如果为空,则用户可以更新配置文件而无需更改当前密码。如果不为空,则存储密码的新值。我的控制器是:

public function storeUpdatedUser(Request $request)
{
    $this->validate($request, ['email' => 'required', 'name' => 'required', 'surname' => 'required', ]);

    $user = User::findOrFail(Auth::user()->id);
    $user->update($request->all());
    $new_password = false;

    if($new_password != ""){
        $new_password = bcrypt($request->new_password);
        $user->password = $new_password;
    }

    $user->save();
    Session::flash('flash_message', 'User updated!');

    return redirect('/');           
}
Run Code Online (Sandbox Code Playgroud)

但不起作用,如果我输入一些值图像,密码也不会更改,请更好地解释 在此处输入图片说明

Fel*_*rte 7

尝试这个:

public function storeUpdatedUser(Request $request)
{
    $this->validate($request, ['email' => 'required', 'name' => 'required', 'surname' => 'required', ]);

    $user = User::findOrFail(Auth::user()->id);
    $user->update($request->all());

    if(!empty($request->input('new_password'))) {
        $new_password = bcrypt($request->input('new_password'));
        $user->password = $new_password;
        $user->save();
    }

    Session::flash('flash_message', 'User updated!');

    return redirect('/');           
}
Run Code Online (Sandbox Code Playgroud)