In my project using Laravel, I need to update the status of a patient when they schedule an appointment with a doctor. The patient can have three statuses: Accept, Waiting (automatically set when the patient selects a date and time), Not Accept. However, when I click on the active button, nothing changes in the database.
Here is the controller for changing the status:
public function changeStatus(Request $request)
{
$user = rdv::where('IDP', $request->input('IDP'))->get();
if (count($user) > 0) {
if ($user->Etat_de_rdv == 'en_attente') {
$user->Etat_de_rdv = 'Accepté';
} else {
$user->Etat_de_rdv = 'Reservé';
}
$user->Etat_de_rdv = $request->input('status');
$user->save();
return response()->json(['success' => 'Status changed successfully']);
}
}
Below is the script:
<script>
$('.toggle-class').change(function () {
var status = $(this).prop('checked') ? 'Accepted' : 'Busy';
var id = $(this).data('IDP');
console.log(id);
console.log(status);
$.ajax({
type: "get",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: 'changeStatus',
data: {
Etat_de_rdv: status,
IDP: id
},
success: function (data) {
console.log(data.success);
}
});
});
</script>
This view displays all appointments seen by the doctor:
<h3> Your patients :</h3>
@foreach($pat as $lo)
@if ($lo->IDD== $med->ID)
<div class="admin">
<div class="info">
<h3> {{ $lo->Nom_et_prénom }} </h3>
<p>{{ $lo->Numéro_de_téléphone }}</p>
<p>{{ $lo->date}}</p>
<p>{{ $lo->time }}</p>
<input data-id="{{$lo->IPD}}" class="toggle-class" type="checkbox" data-onstyle="success"
data-offstyle="danger" data-toggle="toggle" data-on="Accepted"
data-off="Busy" {{ $lo->Etat_de_rdv ? 'checked' : '' }}>
</div>
</div>
@endif
@endforeach
*rdv means: appointment table *IDP: Patient's ID