I enclose a fragment of the edit blade of the Appointment class. The code of the area of interest in question is the following:
<div class="form-group col-md-6 ">
<label for="exampleInputState1">Estado</label>
<select id="exampleInputState1" class="form-control" name="state">
<option value="normal" @if(old('state') == 'normal')selected @endif>normal</option>
<option value="cancelada" @if(old('state') == 'cancelada')selected @endif>cancelada</option>
<option value="aplazada" @if(old('state') == 'aplazada')selected @endif>aplazada</option>
</select>
</div>
Appointment Edit blade
@extends('layouts.app')
@include('layouts.menu')
@section('content')
<div class="container" style="padding:70px 0">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Editar cita</div>
<div class="card-body">
<form action="updateAppointment" method="POST">
<input type="hidden" name="_method" value="PUT">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<input name="id" type="hidden" value="{{ $appointment->id }}"/>
<div class="form-group col-md-10" >
<label for="exampledateAndHour1">Fecha y hora</label>
<input value="{{ old('dateAndHour',$appointment->dateAndHour) }}"
name="dateAndHour" type="datetime-local" class="form-control"
id="exampledateAndHour1" >
</div>
<div class="form-group col-md-6 ">
<label for="exampleInputState1">Estado</label>
<select id="exampleInputState1" class="form-control" name="state">
<option value="normal" @if(old('state') == 'normal')selected @endif>normal</option>
<option value="cancelada" @if(old('state') == 'cancelada')selected @endif>cancelada</option>
<option value="aplazada" @if(old('state') == 'aplazada')selected @endif>aplazada</option>
</select>
</div>
<div class="form-group col-md-10">
<label for="exampleInputHeader1">Título</label>
<input value="{{ old('header',$appointment->header) }}"
type="text"name="header" class="form-control" id="exampleInputHeader1" >
</div>
<div class="form-group col-md-10">
<label for="exampleInputDescription1">Descripción</label>
<input value="{{ old('description',$appointment->description) }}"
type="text" name="description" class="form-control"
id="exampleInputDescription1" >
</div>
<div class="form-group col-md-6">
<label for="patient">ID Paciente</label>
<select name="patient_id" id="exampleInputIDPatient1" class="form-control" required autofocus>
@foreach($patients as $patient)
<option value="{{ $patient->id }}">{{ $patient->id }}</option>
@endforeach
</select>
</div>
<div class="form-group col-md-6">
<label for="doctor">ID Doctor</label>
<select name="doctor_id" id="exampleInputIDDoctor1" class="form-control" required autofocus>
@foreach($doctors as $doctor)
<option value="{{ $doctor->id}}">{{ $doctor->id}}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Guardar</button>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
There is no problem when displaying the old description and header values. However, the selected old value is not displayed in state. For example, when you edit an appointment whose state was initially equal to "canceled", when you open the editing form you have:
I do not know how to solve this problem, and in fact it occurs with other Appointment variables such as dateAndHour. Perhaps the way to obtain the previous data of the select is not the most appropriate?
Edit: capture what appears in source code
I also show dateAndHour so you can see that in that case the previous value is received.
Attached code of the implied methods of the Appointment controller:
a) Edit-AppointmentController:
public function edit(Request $request)
{
//
$appointment = Appointment::findOrFail($request->id);
$doctors = Doctor::all();
$patients = Patient::all();
return view ('appointments/edit')->with('appointment',$appointment)->with('doctors',$doctors)->with('patients',$patients);
}
b) Update-AppointmentController
public function update(Request $request)
{
//
$appointment = Appointment::findOrFail($request->id);
$this->validate($request,[
'doctor_id' => 'required',
'patient_id' => 'required',
'dateAndHour' => 'required',
'state' => 'required',
'header' => 'required',
'description' => 'required|max:500'
]);
$input = $request->all();
$appointment->fill($input)->save();
return Redirect::to('appointments');
}