Home > Back-end >  Laravel 7 - Update user
Laravel 7 - Update user

Time:01-08

I'm having problems with the 'edit' action of my users. I can create new users, but I cannot update them.

  • This is my edit method in the UserController

    /**
         * Show the form for editing the specified resource.
         *
         * @param  \App\User  $user
         * @return \Illuminate\Http\Response
         */
        public function edit($userID)
        {
            //
            $user = User::query()->findOrFail($userID);
            $roles = Role::pluck('nombre_rol','id');
            $departments = Department::all(['id','department_name']);
    
            return view('user.edit',compact('user','roles','departments'));
        }

  • This is the update method in the UserController

    /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \App\User  $user
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $usuarioID)
        {
            $user = User::query()->findOrFail($usuarioID);
    
            $user->update($request->only('name','email','password', 'roles', 'departament_id'));
            $user->roles()->sync($request->roles);
    
            return back()->with('Success','Usuario actualizado con éxito');
        }

  • This is the view for the edit

    <body id="bodyForm">
        <div >
            <h2 >@lang('edit.Editar') @lang('edit.Usuario')</i></h1>
                <h4 >{{$user->name}}</h1>
                    <br>
    
                    <form method="POST" action="{{route('Usuario.update', $user->id)}}  enctype="multipart/form-data"">
                        {!! method_field('PUT') !!}
    
                        @include('user.form')
                        <!--Roles-->
                        <br>
                        <div >
    
                            <div >
                                <h5 >@lang('edit.Roles')</h5>
                                @foreach($roles as $id => $name)
                                <input type="checkbox" value="{{$id}}" {{$user->roles->pluck('id')->contains($id) ? 'checked' : ''}} name="roles[]">
                                {{ $name }}
                                @endforeach
                            </div>
                        </div>
                        <br>
                        <div >
                            
                            <button  type="submit" value="Acttualizar">@lang('edit.Actualizar')</button>
                            <button  onclick="cancel()" value="Cancelar">@lang('edit.Cancelar')</button>
                        </div>
                    </form>
        </div>
    </body>

  • And this is the form that I @include in the edit view.

    <div >
    
        <div >
            <label  for="name">@lang('form.Nombre')</label>
            <input value="{{isset($user->name)?$user->name:old('name')}}" required type="text" name="name" id="name" >
        </div>
    
        <div >
            <label  for="email">@lang('form.Correo')</label>
            <input value="{{isset($user->email)?$user->email:old('email')}}" required type="text" name="email" id="email" >
        </div>
    </div>
    
    <div >
        <div >
            <label >@lang('form.Contraseña')</label>
            <input value="{{--decrypt($usuario->password) ?? old('password')--}}"  type="password" name="password" id="password">
        </div>
        <div >
            <label >@lang('form.Seleccionar')</label>
            <select name="department_id" id="department_id" >
                <option selected disabled value="">@lang('form.Seleccionar')</option>
                @foreach ($departments as $department)
                <option value="{{$department->id}}">{{$department->department_name}}</option>
                @endforeach
            </select>
        </div>
    </div>
  • When I press the edit button I get the error:

419 Page Expired

  • Images of the users table structure

Users Tables

CodePudding user response:

HTML forms only support POST and GET so if u want to perform an action of updating u need to add @method('PUT') between your opening and closing form tags and then add @csrf still inside your form tags

 <form method="POST" action="{{route('user.update', $user->id)}}  enctype="multipart/form-data"">
@method('PUT')
@csrf
</form>

CodePudding user response:

If you're getting this error after submitting edit form, then put @csrf after {!! method_field('PUT') !!}

It should like,

                    <form method="POST" action="{{route('Usuario.update', $user->id)}}  enctype="multipart/form-data"">
                        {!! method_field('PUT') !!}
                        @csrf // For lower version use - {{ csrf_field() }}
                        @include('user.form')
                        <!--Roles-->
                        <br>
                        <div >
    
                            <div >
                                <h5 >@lang('edit.Roles')</h5>
                                @foreach($roles as $id => $name)
                                <input type="checkbox" value="{{$id}}" {{$user->roles->pluck('id')->contains($id) ? 'checked' : ''}} name="roles[]">
                                {{ $name }}
                                @endforeach
                            </div>
                        </div>
                        <br>
                        <div >
                            
                            <button  type="submit" value="Acttualizar">@lang('edit.Actualizar')</button>
                            <button  onclick="cancel()" value="Cancelar">@lang('edit.Cancelar')</button>
                        </div>
                    </form>
  •  Tags:  
  • Related