Change Auth::user values after logged in Laravel 5

Posted 2 years ago by kmdeoliv

Hello,

I need to provide a method to update the name, email and password of a logged a user and I'm using Laravel's authentication method. I created a client controller which has the edit and update resources

<?php

...

    public function edit(User $user)
    {
        if(auth()->user()->type ==1)
        {
          return view('clientes.edit')->withUser($user);
        }
        else
        {
          
          return redirect()->to('/');
        }
    }

    public function update(User $user)
    {
      $user->name = request()->input('name');
      $user->email =  request()->input('email');
      $user->password = \Hash::make($request->input('password'));
      $user->save();
      
      return view('clientes.index')->withUser($user);
    }

...

}

I also have the view to edit the user's data

@extends('layouts.app')

@section('content')

<div class="container">



  <h1>Editar Usuário - {{$user->name}} </h1>
  <p class="lead">Edite este seus dados abaixo</p>
  <hr>

  <div class="row">
      <div class="col-md-8 col-md-offset-2">
          <div class="panel panel-default">
              <div class="panel-heading">Editar usuário</div>
              <div class="panel-body">
                  <form class="form-horizontal" role="form" method="POST" action="{{ route('clientes.update',$user) }}">
                    <input name="_method" type="hidden" value="PATCH">
                    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
                        <div class="form-group">
                            <label for="name" class="col-md-4 control-label">Nome</label>
                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control" name="name" required>
                            </div>
                        </div>
                      <div class="form-group">
                          <label for="email" class="col-md-4 control-label">Email</label>
                          <div class="col-md-6">
                              <input id="email" type="email"  name="email" required>
                          </div>
                      </div>
                      <div class="form-group">
                          <label for="password" class="col-md-4 control-label">Senha</label>
                          <div class="col-md-6">
                              <input id="password" type="password" class="form-control" name="password" required>
                          </div>
                      </div>
                      <div class="form-group">
                          <div class="col-md-6 col-md-offset-4">
                              <button type="submit" class="btn btn-primary">
                                  Editar Usuário
                              </button>
                          </div>
                      </div>
                  </form>
              </div>
          </div>
      </div>
  </div>

</div>

@endsection


The problem is when I click on the button to update the user I'm getting this error MethodNotAllowedHttpException in RouteCollection.php line 233. My route list for clientes is

 POST      | clientes                     | clientes.store       | App\Http\Controllers\[email protected]                           | web,auth     |
|        | GET|HEAD  | clientes                     | clientes.index       | App\Http\Controllers\[email protected]                           | web,auth     |
|        | GET|HEAD  | clientes/create              | clientes.create      | App\Http\Controllers\[email protected]                          | web,auth     |
|        | PUT|PATCH | clientes/{cliente}           | clientes.update      | App\Http\Controllers\[email protected]                          | web,auth     |
|        | DELETE    | clientes/{cliente}           | clientes.destroy     | App\Http\Controllers\[email protected]                         | web,auth     |
|        | GET|HEAD  | clientes/{cliente}           | clientes.show        | App\Http\Controllers\[email protected]                            | web,auth     |
|        | GET|HEAD  | clientes/{cliente}/edit      | clientes.edit        | App\Http\Controllers\[email protected]                            | web,auth

Any help?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.