该路由不支持POST方法。支持的方法:GET,HEAD,PUT

时间:2020-02-12 00:29:22

标签: php jquery laravel

我有一个带有复选框的SALARIES的数据表,并由CHANTIERS填充了一个选择,我想根据选中的行更新在select中选择的CHANTIER的SALARIES CHANTIERS的CHANTERERS,但这给了我错误。 / p>

嗨,我有一个带复选框的SALARIES的数据表,并有一个由CHANTIERS填充的选择,我想根据选中的行更新在select中选择的CHANTIER的SALARIES CHANTIERS的多个数据,但这给了我错误

affectation.blade.php

 <div class="form-group col-md-3">
                  <select class="form-control" id="chantier">
                          <option></option>
                           @foreach($chantiers as $chantier)
                          <option value="{{ $chantier->id }}">{{ $chantier->chantier}}</option>
                           @endforeach
                  </select>
              </div>
              <div class="form-group col-md-4">
               <button class="btn btn-theme update-all" data-url="">Update All</button>
              </div>
<table id="example" class="table table-striped table-bordered" style="width:100%">
                 <thead>
                  <tr>
                    <th><input type="checkbox" id="check_all"></th>
                    <th>nom prenom</th>
                    <th>cin</th>
                    <th>matricule</th>
                    <th>chantier</th>
                  </tr>
                 </thead>
                <tbody>
                  @foreach($salaries as $salarie)
                  <tr id="{{$salarie->id}}">
                    <td><input type="checkbox" class="checkbox" name="customer_id[]" value="{{$salarie->id}}" /></td>
                    <td>{{ $salarie->nom }} {{ $salarie->prenom }}</td>
                    <td>{{ $salarie->cin }}</td>
                    <td>{{ $salarie->matricule }}</td>
                    <td>{{ $salarie->chantier->chantier }}</td>
                  </tr>
                  @endforeach
                </tbody>
</table>

jQuery

   <script type="text/javascript">
    $(document).ready(function () {
        $('#check_all').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".checkbox").prop('checked', true);  
         } else {  
            $(".checkbox").prop('checked',false);  
         }  
        });
         $('.checkbox').on('click',function(){
            if($('.checkbox:checked').length == $('.checkbox').length){
                $('#check_all').prop('checked',true);
            }else{
                $('#check_all').prop('checked',false);
            }
         });
        $('.update-all').on('click', function(e) {

          if(confirm("Are you sure you want to update this?"))
  {
   var id = [];
   var chantier;

   $(':checkbox:checked').each(function(i){
    id[i] = $(this).val();
   });

   if(id.length === 0){
    alert("Please Select atleast one checkbox");
   }else{
    let chantier = $('#chantier').val();
    $.ajax({
     url:"{{ route('salarie.multiple-update') }}",
     method:'PUT',
     headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
     data:{
           id:id,
           chantier:chantier
    },
     success:function(){
      for(var i=0; i<id.length; i++)
      {
      $('tr#'+id[i]).find('td:last-child').html(chantier);
      }
     }

    });
   } 
  }
  else
  {
   return false;
  }

    });
    });
</script>

web.php

Route::PUT('affectation', ['as'=>'salarie.multiple-update','uses'=>'SalarieController@updateMultiple']); 

SalarieController.php

public function updateMultiple(Request $request){
     Salarie::find(explode(',',request('ids')))->each(function($item) {
      $item->update(['chantier_id' => request('chantier_id')]);
     });
     return response()->json(['status'=>true]); 
    }

2 个答案:

答案 0 :(得分:0)

HTTP不支持PUT方法,可以使用POST并将_method = PUT参数添加到数据主体中。即:

<script type="text/javascript"> $.ajax({ url:"{{ route('salarie.multiple-update') }}", method:'POST', headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}, data:{ id:id, chantier:chantier, _method: "PUT" }); </script>

答案 1 :(得分:-1)

PHP本身不支持PUT HTTP方法。为此,您可以使用POST并将_method = PUT参数添加到queryString中。即:

 <script type="text/javascript">
    $.ajax({
     url:"{{ route('salarie.multiple-update') }}" + '?_method=PUT',
     method:'POST',
     headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
     data:{
       id:id,
       chantier:chantier
    });
</script>