Many Rodríguez
Many Rodríguez

Reputation: 65

Laravel 7 - Update user

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 class="container">
        <h2 class="text-center txt-unam-gold">@lang('edit.Editar') @lang('edit.Usuario')</i></h1>
            <h4 class="text-center text-secondary">{{$user->name}}</h1>
                <br>

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

                    @include('user.form')
                    <!--Roles-->
                    <br>
                    <div class="row justify-content-center text-center">

                        <div class="col-md-6">
                            <h5 class="txt-unam text-center text-primary">@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 class="text-center">
                        
                        <button class="btn btn-warning" type="submit" value="Acttualizar">@lang('edit.Actualizar')</button>
                        <button class="btn btn-danger" 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 class="row justify-content-center py-3">

    <div class="col-md-5 mr-2">
        <label class="txt-unam text-primary" for="name">@lang('form.Nombre')</label>
        <input value="{{isset($user->name)?$user->name:old('name')}}" required type="text" name="name" id="name" class="form-control text-muted">
    </div>

    <div class="col-md-5">
        <label class="txt-unam text-primary" for="email">@lang('form.Correo')</label>
        <input value="{{isset($user->email)?$user->email:old('email')}}" required type="text" name="email" id="email" class="form-control text-muted">
    </div>
</div>

<div class="row justify-content-center py-1">
    <div class="col-md-5 mr-2">
        <label class="txt-unam text-primary">@lang('form.Contraseña')</label>
        <input value="{{--decrypt($usuario->password) ?? old('password')--}}" class="form-control" type="password" name="password" id="password">
    </div>
    <div class="col-md-5">
        <label class="txt-unam text-primary">@lang('form.Seleccionar')</label>
        <select name="department_id" id="department_id" class="form-control">
            <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

Upvotes: 1

Views: 612

Answers (2)

Kaddu Livingstone
Kaddu Livingstone

Reputation: 1594

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)}} class="user" enctype="multipart/form-data"">
@method('PUT')
@csrf
</form>

Upvotes: 0

Masudul Hasan Shawon
Masudul Hasan Shawon

Reputation: 522

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)}} class="user" enctype="multipart/form-data"">
                        {!! method_field('PUT') !!}
                        @csrf // For lower version use - {{ csrf_field() }}
                        @include('user.form')
                        <!--Roles-->
                        <br>
                        <div class="row justify-content-center text-center">
    
                            <div class="col-md-6">
                                <h5 class="txt-unam text-center text-primary">@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 class="text-center">
                            
                            <button class="btn btn-warning" type="submit" value="Acttualizar">@lang('edit.Actualizar')</button>
                            <button class="btn btn-danger" onclick="cancel()" value="Cancelar">@lang('edit.Cancelar')</button>
                        </div>
                    </form>

Upvotes: 1

Related Questions