Laravel 5 update user record without password

Essentially I have a page where I can update user posts.

        {!! Form::label('username', 'Username:') !!}
        {!! Form::text('username', null, ['class'=>'form-control'])!!}

        {!! Form::label('password', 'Password:') !!}
        {!! Form::password('password',['class'=>'form-control'])!!}

        {!! Form::label('name', 'Name:') !!}
        {!! Form::text('name', null, ['class'=>'form-control'])!!}

        {!! Form::label('email', 'Email:') !!}
        {!! Form::text('email', null, ['class'=>'form-control'])!!}

      

Now there are two problems I am facing:

1) If I don't want to change the user's password and just edit the name, for example, it will update the user's password to be blank (or ""). This will be because the textbox is empty after submitting.

This is what I tried in my controller with no joy

public function update($id, Request $request)
{
    $user = User::findOrFail($id);
    $newPassword = $request->only('password');

    if(empty($newPassword)){
        $user->update($request->except('password'));
    }else{
        $user->update($request->all());
    }
    return redirect('users');
}

      

Does anyone know how I can solve this? I have a way to deal with these things.

For reference, this is the laravel 5 I am using.

+3


source to share


1 answer


use: $request->get('password');

instead: $request->only('password');

Why:



$request->only();

- returns an array with the specified fields

In this case, it returns an array with only one key and an empty value, but the array is not empty.

+4


source







All Articles