Home » Php » php – explode() expects parameter 2 to be string, array given multipleUpdate

php – explode() expects parameter 2 to be string, array given multipleUpdate

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a datatable of the SALARIES with checkboxes and a select filled in by the CHANTIERS, I want to update multiple the CHANTIERS of the SALARIES by CHANTIER selected in select, according to the cheked line, but it gives me error. i think error in function updateMultiple.

SalarieController.php

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

wep.php

Route::get('affectation','[email protected]'); 
Route::post('affectation', ['as'=>'salarie.multiple-update','uses'=>'[email protected]']); 

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>

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') }}" ,
     headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
     method:'post',
     data:{
           id:id,
           chantier:chantier,
           "_token": "{{ csrf_token() }}"
    },
     success:function(){
      for(var i=0; i<id.length; i++)
      {
      $('tr#'+id[i]).find('td:last-child').html(chantier);
      }
     }

    });
   } 
  }
  else
  {
   return false;
  }

    });
    });
</script>
How to&Answers: