0

So i want to make admin can change any user's password. but i got this error

Call to a member function update() on null

I have no idea what to do

this is my controller

public function resetPassword(Request $req, $id)
{
    $req->validate([
        'new_password' => ['required'],
        'new_confirm_password' => ['same:new_password'],
    ],
    [
        'new_password.required' => 'Password Baru Harus Diisi !',
        'new_confirm_password.same' => 'Password Baru Harus Sama Dengan Confirm Password !',
    ]);
    User::find($id)->update(['password'=> Hash::make($req->new_password)]);
    return redirect('/admin/list_user')->with('status', 'Password Berhasil di Ubah !');
}

this my route

Route::put('/admin/{id}/reset/password', 'PageController@resetPassword')->name('resetPassword');

this is my view modal

<div class="modal fade" id="resetModal" tabindex="-1" role="dialog" aria-labelledby="resetModal" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Reset Password</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="/admin/{id}/reset/password" method="POST">
          {{csrf_field()}}
          {{ method_field('PUT') }}
          <div class="form-group">
            <label for="password">Password Baru</label>
            <input name="new_password" type="password" class="form-control" id="new_password" required>
          </div>
          <div class="form-group">
            <label for="password">Confirm Password</label>
            <input name="new_confirm_password" type="password" class="form-control" id="new_confirm_password" required>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" data-dismiss="modal">Tutup</button>
          <button type="submit" class="btn btn-primary">Buat</button>
        </form>
      </div>
    </div>
  </div>
</div>
</div>
4
  • Looks like no user with that id exists in the database. If you change find() to findOrFail() you should get an error emphasizing this. Commented May 18, 2021 at 15:38
  • 1
    User::find($id) is null, so you can't call update, as null->update(...) is not valid... What is $id? Do you have a User in your database that has an id of $id? Commented May 18, 2021 at 15:38
  • @TimLewis actually i want to call 'id' of the user that admin wants to change, but i dont know how to do that, can you please tell me how to do that sir? Commented May 18, 2021 at 15:42
  • See the answer below. The string /admin/{id}/reset/password is not valid as your action="...", it should be action="{{ url('/admin/1/reset/password') }}" or action="{{ route('resetPassword', ['id' => 1]) }}", passing the expected id along (used 1 as an example, but you need to define the correct id, either from a variable like $id, or from a User, like $user->id, etc etc) Commented May 18, 2021 at 15:44

1 Answer 1

2

<form action="/admin/{id}/reset/password" method="POST">

You're not passing any id. The route picks up '{id}' as the id, then tries to find an User with the id '{id}' and finds none. (->first() returns null).

Just change that opening form tag's action attribute:

<form action="{{ route('resetPassword', ['id' => Auth::id()]) }}" method="POST"> (or instead of Auth::id() the user_id you're trying to update.


You could also use findOrFail($id) instead of find($id) so you'll get a clearer error message if an User is not found.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.