Skip to content
Advertisement

Laravel: problem with password not updated with hash password in database

When I register a new user sign up, it save the password to database with hashed password. But when I go edit the user from the admin dashboard, the edit function work perfectly, but the password did not store or save as a hashed password, it save as plain text.

This is link to image show in database modification

This is the userController:

public function edit($id)
{
    $user = $this->userRepository->find($id);

    if (empty($user)) {
        Flash::error('User not found');

        return redirect(route('users.index'));
    }

    return view('users.edit')->with('user', $user);
}

/**
 * Update the specified User in storage.
 *
 * @param int $id
 * @param UpdateUserRequest $request
 *
 * @return Response
 */
public function update($id, UpdateUserRequest $request)
{
    $user = $this->userRepository->find($id);

    if (empty($user)) {
        Flash::error('User not found');

        return redirect(route('users.index'));
    }

    $user = $this->userRepository->update($request->all(), $id);

    Flash::success('User updated successfully.');

    return redirect(route('users.index'));
}

This is my fields.blade.php:

<!-- Name Field -->
 <div class="form-group col-sm-6">
  {!! Form::label('name', 'Name:') !!}
  {!! Form::text('name', null, ['class' => 'form-control']) !!}
 </div>

   <!-- Email Field -->
    <div class="form-group col-sm-6">
     {!! Form::label('email', 'Email:') !!}
     {!! Form::email('email', null, ['class' => 'form-control']) !!}
    </div>


   @push('scripts')
       <script type="text/javascript">
          $('#email_verified_at').datetimepicker({
             format: 'YYYY-MM-DD HH:mm:ss',
             useCurrent: true,
             sideBySide: true
       })
      </script>
     @endpush

   <!-- Password Field -->
      <div class="form-group col-sm-6">
        {!! Form::label('password', 'Password:') !!}
        {!! Form::password('password', ['class' => 'form-control']) !!}
      </div>

        <!-- Role Id Field -->
          <div class="form-group col-sm-6">
             {!! Form::label('role_id', 'Role Id:') !!}
             {!! Form::number('role_id', null, ['class' => 'form-control']) !!}
         </div>



     <!-- Submit Field -->
       <div class="form-group col-sm-12">
         {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
     <a href="{{ route('users.index') }}" class="btn btn-default">Cancel</a>
      </div>

Another thing: I’m using infyom Laravel generator.

This is the register controller (I make it by command make:auth):

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

This is the userRepository code:

     <?php

       namespace AppRepositories;

        use AppModelsUser;
        use AppRepositoriesBaseRepository;

            /**
           * Class UserRepository
           * @package AppRepositories
           * @version June 23, 2020, 3:11 pm UTC
             */

        class UserRepository extends BaseRepository
           {
            /**
            * @var array
            */
protected $fieldSearchable = [
    'name',
    'email',
    'password',
    'role_id'
];

/**
 * Return searchable fields
 *
 * @return array
 */
public function getFieldsSearchable()
{
    return $this->fieldSearchable;
}

/**
 * Configure the Model
 **/
public function model()
{
    return User::class;
}
}

Advertisement

Answer

Add the update method to the UserRepository

protected function update(array $data,$id)
{
    return User::where($id)->update([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement