Multiple variables by json and ajax with Laravel

0

I explain what I have tried to do, I have created 2 select dependents, one is called type of person (Natural Person and Legal Entity) and the other company , select the type of person, the next select (company) will load the corresponding data. Until then I'm fine. Then when I select a company I need to upload data from the company table, the detail is that at the same time I must load data from another table in which is the ID of the company, in this case, business turn. / p>



   $('#empresa').on('change', function(e){
        console.log(e);
        var empresa_id = e.target.value;
        //alert(empresa_id);
        $.get('select_empresa?empresa_id=' + empresa_id,function(data) {
          console.log(data);
          $('#empresa_nombre').empty();
          $('#empresa_ruc').empty();
          $('#empresa_mail').empty();
          $('#empresa_tel').empty();
          $('#empresa_dir').empty();
          $('#empresa_giro').empty();
          document.getElementById('infoempresa').style.display = 'block';

          $.each(data, function(datos_empresa, empObj){
            $('#empresa_nombre').val(empObj.companyname_cny);
            $('#empresa_ruc').val(empObj.ruc_cny);
            $('#empresa_mail').val(empObj.email_cny);
            $('#empresa_dir').val(empObj.address_cny);
            $('#empresa_tel').val(empObj.phone_cny);
            //$('#empresa_giro').val("Giro de Negocio"); 
          })

        });
      });

In this script I send the data to the form that contains the company data, the problem is with the field Business Turning

function datos_empresa(Request $request)
{
  $empresa_id = Input::get('empresa_id');
  $empresa = Empresa::where('id','=',$empresa_id)->get();

  $giro = negocio::where('empresa_id','=',$empresa_id)->get();
  //dd($giro);

  return Response::json(['empresa'=>$empresa, 'giro'=>$giro]);
}

This is my function of the controller to load the data, the detail is that it does not show me anything.

    
asked by Luis Medina 03.05.2018 в 01:51
source

1 answer

0

I would do it this way

$("#campoSelect").select2({
                placeholder: "Selecciona una opción",
                language: "es",
                width: '100%',
                ajax: {
                    url: "{{ route('controlador.busqueda') }}",
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            q: params.term,
                            parametro1: variable,
                            parametro2: $('#input').val(),
                            parametro3: document.querySelector('#check' + ':checked').value
                        };
                    },
                    processResults: function (data) {
                        return {
                            results: data
                        };
                    },
                    cache: true
                },
                templateResult: locationResultTemplater,
                templateSelection: locationSelectionTemplater
            });

Here is the search format

function locationResultTemplater(data) {
            if (data.text !== undefined) {
                return data.text
            }
            return 'Campo: ' + data.nombre;
        }

And finally the selected one

function locationSelectionTemplater(data) {
    if (data.text !== "") {
        return data.text
    }
    return data.nombre;
}

And the controller

            Route::get('busqueda', 'Controller@busqueda')->name('controlador.busqueda');

    public function pieza()
    {
        $q       = trim(e(Input::get('q')));
        $p1 = trim(e(Input::get('parametro1')));
        $p2 = trim(e(Input::get('parametro2')));
        $p3 = trim(e(Input::get('parametro3')));

        $results = Modelo::where('nombre', 'LIKE',
            '%' . $q . '%')
->where('p1', $p1)
->where('p2', $p2)
->where('p3', $p3)
->take(100)->get();

        return Response::json($results);
    }
    
answered by 03.05.2018 / 09:42
source