Update class data with Array in Laravel

Asked

Viewed 77 times

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.

  • Hello good morning, sorry. My doubt is how to edit all teachers, subjects, schedules and day of the week at once

1 answer

0

The way you defined the fields of the type select will only allow a value to be selected. If you want multiple options to be selected, you will have to set in the field select the attribute Multiple, as the example below:

<select name="" id="" multiple>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
</select>

The documentation of such a type can be verified in that link.

Due to this condition, the loop for within the controller will iterate only once.

Obs.: I am considering that the submission of form via AJAX and the controller back-end are working properly.

  • Thanks for the help, but he still takes the first field.

  • Look, I took a good look at your question and your code. My suggestion is that you rephrase the question. I can’t really understand what you’re in need of. The route used by the request via AJAX is used to update a specific record (referring to the ID parameter). The controller uses this same ID to update different records. That’s why it’s confused.

  • Thanks for the tip, but the idea was to get the id of the class, where it contains several schedules, day of the week, teachers and discipline and update these records.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.