I'm beginner in Ajax. I'm getting 'Undefined variable $doctor' If I have not any record in database I got this error but It works when I delete the line starting with 'prepend' in Ajax script. Where is my problem?
My Controller is:
public function doctors(){
$doctors = Doctor::where('polikliniks',Auth::user()->id)->orderBy('id','DESC')->get();
return view('panel.doctors',['doctors'=>$doctors]);
}
My blade is:
@foreach($doctors as $doctor)
<tr id="sid{{$doctor->id}}">
<input type="hidden" value="{{$doctor->id}}" name="id">
<td>
<p>{{$doctor->name}}</p>
</td>
</tr>
@endforeach
My ajax is:
$('#doctorForm').submit(function(e){
e.preventDefault();
let id = $('#id').val();
let name = $("#name").val();
$.ajax({
url: "{{route('add_doctorPost')}}",
type:"POST",
data:{id:id,name:name,_token:'{{ csrf_token() }}'},
success:function(response){
if(response){
$("#doctorTable tbody").prepend('<tr><td>p>'+ response.name +'</p></td><td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#appointmentModal{{$doctor->id}}" ></button><td><a href="javascript:void(0)" data-type="confirm" class="btn btn-danger" onclick="deleteConfirmation({{$doctor->id}})" </a></td> </tr>')
$('#doctorForm')[0].reset();
$('addDoctor').modal('hide');
}
}
});
});