0
votes

I'm new to Laravel (I'm using version 8) and I've been reviewing this error for a long time and I don't know what else to try. I have a resource controller called ** UserController.php ** and I set the path in ** web.php ** as follows: Route::resource('usuarios', App\Http\Controllers\UsuarioController::class);

The resource paths are: enter image description here

The user can enter their profile and modify their fields through this menu option that invokes the edit() method of the controller: enter image description here

So far everything works very well, but when the user enters to edit in the ** edit.blade.php ** form, and selects the "update" button that calls the method ** update () ** of the controller, it jumps to me the error: BadMethodCallException Method App\Http\Controllers\UsuarioController::update does not exist.

This is edit.blade.php:


@section('title', 'Mi Perfil')
 

@section('intro')  



@endsection

<br><br>
<style type="text/css">

h5 {text-align: center}
nav.navbar {
    background-color: #34495E;
}
</style>


 

@section('content0')

<div class="container3">

 <div class="bShadow3 bShadow-33">

    
                    <form method="POST" action="{{route('usuarios.update',[Auth::user()->slug]) }}">
                        @method('PUT')
                        @csrf
                        <br>
                        <h5>Mi perfil</h5>
                           
                        <div class="form-group ml-5 mr-5">
                            <label for="name">{{ __('Nombre de usuario') }}</label>
                            <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{Auth::user()->name}}" autocomplete="name" maxlength="30" autofocus placeholder="Introduce tu nombre de usuario" required>
                                @error('name')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                        </div>

                        <div class="form-group ml-5 mr-5">
                            <label for="name">{{ __('Dirección de correo:') }}</label><br>
                            <label for="email">{{Auth::user()->email}}</label>
                           
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col text-center">
                                <button type="submit"  class="btn2 mt-2 mb-3">
                                    {{ __('Actualizar') }}
                                </button>
                                <button type="default" class="btn2 mt-2 mb-3">
                                    {{ __('Cancelar') }}
                                </button>

                            </div>
                        </div>
                    </form>



                </div>
            </div>
        </div>
    </div>
</div>
@endsection

This is UsuarioController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Role;


class UsuarioController extends Controller
{
   
    public function __construct()
    {
        //permitir solo usuarios autenticados
        $this->middleware('auth');
    }



    public function index()
    {
        //
    }

   

    public function create()
    {
        //
    }

   
    public function store(Request $request)
    {
        

    }

    
    public function show($id)
    {
         
    }

    
    public function edit($id)
    {
       $usuario=User::find($id);
       if ($usuario== null)
            return Redirect::to('usuario');
 
        return view('usuarios.edit')->with('id',$id);
   
    }

    

    public function update(Request $request, $id)
    {
       $user = User::find($id);
       $user->name = Input::get('name');
       $user->email = Input::get('email');
       $user->password = Input::get('password');      
       $user->save();  
    }  

   
    public function destroy($id)
    {
        //
    }



    
}

If someone can give me a hand, I will thank them very much!

2
Why you need to call Auth::user()->slug on blade route method? You can simply get it on controllersta
What does the full controller file look like? What is the namespace? What is the filename, and where does it live in the file structure?James Clark Developer
Hi, now I put all the complete code of UsuarioController.phpCeci

2 Answers

0
votes

I do not know exactly where the problem comes from but I think

1: change route

{{route('usuarios.update',auth()->user()) }}

2: It needs a model but you send slug usuarios/{usuario}

0
votes

Thanks for the answers, I already fixed the error.

I had two controllers using the same controller class and I was working with one and laravel with another.