I am using a form and I don’t know why I can edit with it the values except in the fields apellido
and cedula
.
I am using the same logic in all the form fields so I dunno what can be causing it.
I’m gonna post the code of the view, controller, and model.
View
@extends('layouts.app') @section('content') <div class="container"> <h1>Editar Médico</h1> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Register') }}</div> <div class="card-body"> <form method="POST" action="{{ route('medico.update', $medico) }}"> @csrf @method('PUT') <div class="form-group row"> <label for="nombre" class="col-md-4 col-form-label text-md-right">{{ __('Nombre') }}</label> <div class="col-md-6"> <input id="nombre" type="text" class="form-control @error('nombre') is-invalid @enderror" name="nombre" value="{{ $medico->nombre}}" required autocomplete="nombre" autofocus> @error('nombre') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <label for="apellido" class="col-md-4 col-form-label text-md-right">{{ __('Apellido') }}</label> <div class="col-md-6"> <input id="apellido" type="text" class="form-control @error('apellido') is-invalid @enderror" name="apellido" value="{{ $medico->apellido}}" required autocomplete="apellido" autofocus> @error('apellido') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <label for="cedula" class="col-md-4 col-form-label text-md-right">{{ __('Cédula') }}</label> <div class="col-md-6"> <input id="cedula" type="text" class="form-control @error('cedula') is-invalid @enderror" name="apellido" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus> @error('cedula') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('Email') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ $medico->email}}" required autocomplete="email" autofocus> @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <label for="telefono" class="col-md-4 col-form-label text-md-right">{{ __('Teléfono') }}</label> <div class="col-md-6"> <input id="telefono" type="text" class="form-control @error('telefono') is-invalid @enderror" name="telefono" value="{{ $medico->telefono}}" required autocomplete="telefono" autofocus> @error('telefono') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <label for="direccion" class="col-md-4 col-form-label text-md-right">{{ __('Dirección') }}</label> <div class="col-md-6"> <input id="direccion" type="text" class="form-control @error('direccion') is-invalid @enderror" name="apellido" value="{{ $medico->direccion}}" required autocomplete="direccion" autofocus> @error('direccion') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <label for="ciudadResi" class="col-md-4 col-form-label text-md-right">{{ __('Ciudad de Residencia') }}</label> <div class="col-md-6"> <input id="ciudadResi" type="text" class="form-control @error('ciudadResi') is-invalid @enderror" name="apellido" value="{{ $medico->ciudadResi}}" required autocomplete="ciudadResi" autofocus> @error('ciudadResi') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <label for="fechaNacimiento" class="col-md-4 col-form-label text-md-right">{{ __('Fecha de Nacimiento') }}</label> <div class="col-md-6"> <input id="fechaNacimiento" type="date" class="form-control @error('fechaNacimiento') is-invalid @enderror" name="fechaNacimiento" value="{{ $medico->fechaNacimiento}}" required autocomplete="fechaNacimiento" autofocus> @error('fechaNacimiento') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row"> <label for="genero" class="col-md-4 col-form-label text-md-right">{{ __('Genero') }}</label> <div class="col-md-6"> <input id="genero" type="text" class="form-control @error('genero') is-invalid @enderror" name="genero" value="{{ $medico->genero}}" required autocomplete="genero" autofocus> @error('genero') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="form-group row mb-0"> <div class="col-md-6 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Editar') }} </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection
Controller
public function updateMedico(Request $request, $id) { $medico = Persona::findOrFail($id); $medico->fill($request->all()); if($medico ->save()) { return redirect()->route('personaMostrarMedicos'); } else { return redirect()->route('medico.edit'); } }
Model
public $timestamps =false; protected $fillable = [ 'nombre', 'apellido', 'cedula', 'email', 'telefono', 'direccion', 'ciudadResi', 'fechaNacimiento', 'genero', 'estado', 'idTipoPersona' ];
I have no idea what could be wrong because only those 2 fields I mention are the ones I can’t update, the other ones are fine.
In case it can helps this is also my database model
After using dd($request->all());
in the controller this is the output
What I have in the form before clicking the update button and the output of dd($request->all())
Advertisement
Answer
I am not sure if this is the problem, but you have 2 name="apellido"
, check cedula
, it has that name
, so that is wrong.
Remember that when you send a form, the way to get the value is going to use the name
property and not the id
as that is pure CSS.
Change this:
<input id="cedula" type="text" class="form-control @error('cedula') is-invalid @enderror" name="apellido" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus>
To this:
<input id="cedula" type="text" class="form-control @error('cedula') is-invalid @enderror" name="cedula" value="{{ $medico->cedula }}" required autocomplete="cedula" autofocus>