How to pass an array by ajax to the controller?

Asked

Viewed 141 times

0

I have an ajax that sends data from my selects to my controller and from there I do a check and query, so far everything is working perfectly. The problem is that the last select that sends is an array, because it is a select Multiple, and Dái am not being able to pass it correctly. Someone to give a hint? I know it’s easy, but I’m not getting it. Detail: I need this Function (d), because I think it is important in yajra Datatable.

View:

<div class="form-group">
     {{ Form::select('subunidade[]', $subunidades, null, array('class' => 'form-control js-example-basic-multiple', 'id' => 'subunidade', 'multiple' => 'multiple')) }}
</div>

Ajax:

<script type="text/javascript">

      var subunidades = new Array();

      $("select[name='subunidade[]']").each(function(){
         subunidades.push($(this).val());
      });

    var oTable = $('#example').DataTable({
           dom: "<'row'<'col-xs-12'<'col-xs-6'l><'col-xs-6'p>>r>"+
               "<'row'<'col-xs-12't>>"+
               "<'row'<'col-xs-12'<'col-xs-6'i><'col-xs-6'p>>>",
           processing: true,
           serverSide: true,
           ajax: {
               url: 'filterBusca',
               data: function (d) {
                   d.nome = $('input[name=nome]').val();
                   d.operador = $('select[name=operador]').val();
                   d.idade = $('input[name=idade]').val();
                   d.unidade = $('select[name=unidade]').val();
                   d.subunidade = $('select[name=subunidade').val();
               }
           },
           columns: [
               {data: 'id', name: 'id'},
               {data: 'matricula', name: 'matricula'},
               {data: 'nome', name: 'nome'},
               {data: 'cpf', name: 'cpf'},
               {data: 'idade', name: 'idade'},
               {data: 'unidade', name: 'unidade'},
               {data: 'sub_unidade', name: 'sub_unidade'},
           ]
       });


       $('#Filtrar').on('submit', function(e) {
           oTable.draw();
           e.preventDefault();
       });

  </script>

Controller:

if ($subunidade = $datatables->request->get('subunidade')) {
      $datatables->whereIn('sub_unidade.id', "$subunidade");
  • It’s not going as array to the controller or it’s not going at all?

1 answer

0

I managed to settle by making the following changes:

I added [] in the name getting subunit[]:

<div class="form-group">
     {{ Form::select('subunidade[]', $subunidades, null, array('class' => 'form-control js-example-basic-multiple', 'id' => 'subunidade', 'multiple' => 'multiple')) }}
</div>

In ajax I captured not by select name, but by id:

d.subunidade = $("#subunidade").val();

And finally, to make the consultation in the controller I made the second:

//Recebi os dados do select
  $dataForm = $datatables->request->get('subunidade');
  //Cria a variavel para receber o array
  $arrayForm = null;
  //Um contador para carregar os dados do array na nova variavel $arrayForm
  for($i = 0; $i<count($dataForm); $i++){
    $arrayForm[] = array("$dataForm[$i]");
  }
  //Verifico
  if ($subunidade = $datatables->request->get('subunidade')) {
  //Faço a consulta usando o whereIn que é capaz de verificar a condição com array
      $datatables->whereIn('sub_unidade.id', $arrayForm);
  }

Simbora, we are together!

Browser other questions tagged

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