Nested lists in Laravel 5.5

2

I'm working on laravel 5.5

I have these two lists:

1- The first is a list of operating systems

{!! Form::mySelect('id_so', 'Sistema Operativo', App\SistemasOperativos::pluck('nombre', 'id')->toArray(), null, ['class'=>'chosen', 'placeholder' => 'Escoge una opción']) !!}

2- The second one, you should list the versions according to the operating system

{!! Form::mySelect('id_version', 
               'Version S.O:', 
                App\SoVersiones::select(DB::raw("version AS version"), "id","id_so")->where('id', '=', 1)-> pluck('version', 'id','id_so')->toArray(),       
                null, 
               ['class'=>'chosen', 'placeholder' => 'Escoge una opción']) !!}

I got to this point, since it brings me the versions because I call them with an integer value, A value burned in the Form, but I can not find the way to bring the value of the first mySelect so that it compares with the vlaor that I am burning and so, the query is successful.

Attached image

Could someone help me?

Thanks for reading, happy day.

EDIT:

Hi, I managed to bring the value of the first field into a variable, but at the time of making the comparison it gives me an error.

First List:

{!! Form::mySelect('id_so', 'Sistema Operativo:',$worktypes_list= App\SistemasOperativos::pluck('id', 'nombre')->toArray() , null, ['class'=>'chosen', 'placeholder' => 'Escoge una opción']) !!}

Second list:

{!! Form::mySelect('id_version', 
               'Version S.O:', 
                App\SoVersiones::select(DB::raw("version AS version"), "id_so")->where('id', '=', $worktypes_list)-> pluck('version', 'id_so')->toArray(),       
                null, 
               ['class'=>'chosen', 'placeholder' => 'Escoge una opción']) !!}

But I get this error:

When the ID's could match, The problem I see is that you are doing the query once, instead of throwing values as soon as you select some value from the first field, How could you correct this?

Thanks for reading.

    
asked by Omar Noa 22.08.2018 в 19:55
source

2 answers

1

I would recommend you populate your second select based on what the user selects from the first select, example:

HTML (I'll put it in HTML because I do not like laravel collective):

<select name="so" id="so">
   <option value="">Seleccionar sistema</option>
   {{-- Tomando en cuenta que estás enviando la variable sistemas desde tu controlador a esta vista, la cual, contiene los sistemas registrados en tu BD --}}
   @foreach($sistemas as $sistema)
      <option value="{{ $sistema->id }}">{{ $sistema->nombre }}</option>
   @endforeach
</select>

<select name="version" id="version">
   <option value="">Selecciona un sistema operativo primero</option>
</select>

Javascript (using jQuery):

var rutaConsulta = "{{ route('ruta.consulta.so') }}";
    $(document).ready(function(){
       selectChange();
    });

function selectChange(){
    $('#so').on('change', function(e){
       var idSo = $(this).val();
       ajaxSelect(idSo);
    });
}

function ajaxSelect(id){
    $.ajax({
            type: 'POST',
            url: rutaConsulta,
            data: {id: id},
            dataType: 'json',
            beforeSend: function(){
            }
        }).done(function(response) {
            var html = '<option value="">Selecciona una opción</option>';
            $.each(response.versiones, function(i, elem){
               html += '<option value="'+ elem.id +'">'+ elem.nombre +'</option>'
            });
            $('#version').html(html);
        }).fail(function(data) {

        });
}

In your PHP driver:

public function consultarVersiones(Request $request){
   $id_so = $request->id;
   $versiones = SoVersiones::where('id_so', $id_so)->get();


   $respuesta = array();
   $respuesta['versiones'] = $versiones->toArray();   
   return response()->json($respuesta);
}
    
answered by 23.08.2018 / 00:14
source
0

You can try filtering the second select with the id of the first select.

{!! Form::mySelect('id_version', 
               'Version S.O:', 
                App\SoVersiones::select(DB::raw("version AS version"), "id","id_so")->where('id_so', '=', 'resultado_primer_select')-> pluck('version', 'id','id_so')->toArray(),       
                null, 
               ['class'=>'chosen', 'placeholder' => 'Escoge una opción']) !!}

Thus the second select will only bring the versions of the related operating system.

    
answered by 22.08.2018 в 22:10