1
I have to update several records in mysql database via ajax in Laravel.
There is a list of many teachers with many subjects, however they are part of a class in specific.
If anyone can help me very much thank you.
View:
<select id="materia_id[]" name="materia_id[]" class="form-control" required="true" >
<option value="{{$materia->id}}"> {{$materia->nm_materia}} </option>@foreach($materias as $mat )<option value="{{$mat->id}}">{{$mat->nm_materia}}</option>@endforeach</select>
<select id="professor_id[]" name="professor_id[]" class="form-control" required="true" >
<option value="{{$professor->id}}"> {{$professor->nm_professor}} </option>@foreach($professores as $prof )<option value="{{$prof->id}}">{{$prof->nm_professor}}</option>@endforeach</select>
Ajax:
<script>
var id = $("#id").val();
$("#fEdit").on('submit',function(e){
e.preventDefault();
console.log(id)
var dados = $('#fEdit').serialize();
$.ajax({
type:'PUT',
url:'/turma/atualizahorario/'+id,
data:dados,
success:function(response){
console.log(response);
//location.reload();
},
error:function(error){
console.log(error);
}
});
})
</script>
Controller:
public function update(Request $request,$id){
//so atualiza todos os registro com o valor do ultimo registro
for ($i=0; $i<count($request->materia_id); $i++) {
DB::table('horarioturmaprofessor')
->where('horarioturma_id',$id)
->update([
'diasemana_id'=>$request->diasemana_id[$i],
'horario_id'=>$request->horario_id[$i],
'materia_id'=>$request->materia_id[$i],
'professor_id'=>$request->professor_id[$i],
]);
}
}
But what is your doubt? You simply said what you need to do, you did not say what your doubt is about this.
– Paulo Martins
Hello good morning, sorry. My doubt is how to edit all teachers, subjects, schedules and day of the week at once
– Nilson Rocha