Matthew Smart
Matthew Smart

Reputation: 1319

Laravel 5 updating user record with out password

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

        {!! 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 2 problems I am facing:

1) If I do not want to change the users password and just edit the name for example, it will update the users password to be empty (or ""). This will be because the text box is empty upon submission.

This is what I have 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 tackle this? I'm hopping laravel has a way to deal with these things.

For reference it is laravel 5 that I am using.

Upvotes: 4

Views: 4359

Answers (1)

D. Cichowski
D. Cichowski

Reputation: 777

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

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

Why:

$request->only(); - returns array with listed fields

In this case it returns array with only one key and empty value, but array isn't empty.

Upvotes: 6

Related Questions