Consultation in field of the same Laravel table 5.5

1

I'm working the Laravel 5.5.

I have a table called servers, in this table I insert both physical and virtual servers, and identify them through the following field.

{!! Form::mySelect('id_uso', 'Uso', [0 => 'Escoge una opción'] + App\Usos::pluck('nombre', 'id')->toArray(), null, ['id' => 'listaUsos', 'class'=>'chosen']) !!}

And I have another field called Parent Server, which lists the servers that are in the same table.

{!! Form::mySelect('id_padre', 'Servidor padre', App\Servidores::pluck('nombre', 'id')->toArray(), null, ['class'=>'chosen']) !!}

The problem is that if in the Use field the Virtual option is chosen, In the Server Parent field you should only list the name of the servers listed as Physical.

Could someone tell me how to do it?

Try to make a CONCAT, so that when choosing the value of the drop-down list, it will bring the ID of whether it is physical or virtual, to be able to identify it, but clearly it is not practical, and I would like to learn how to make that particular query.

    
asked by Omar Noa 16.08.2018 в 16:51
source

1 answer

1

Reading the documentation I was able to add the where to the list I was doing, and I'm like this:

{!! Form::mySelect('id_padre', 
               'Servidor padre:', 
 App\Servidores::select(DB::raw("CONCAT(hostname, ' - ', id_uso) AS 
                 hostname_version"), "id")                                
                 ->where('id_uso', '=', 1)
                 -> pluck('hostname_version', 'id')
                 ->toArray(), null, 
 ['required','class'=>'requerido-con-virtual', 'style' => 'width: 100%;', 
  'placeholder' => 'Escoge una opción']) !!}

This option is used for the condition and additionally the CONCAT , in case someone can help.

If you only want the value of the column, I did it like this:

{!! Form::mySelect('id_padre', 
               'Servidor padre:', 
 App\Servidores::select(DB::raw("hostname AS hostname"), "id")
                ->where('id_uso', '=', 1)
                ->pluck('hostname', 'id')
                ->toArray(), null, 
 ['required','class'=>'requerido-con-virtual', 'style' => 'width: 100%;', 
  'placeholder' => 'Escoge una opción']) !!}

There you can see only those that are physical servers appear to me.

    
answered by 16.08.2018 / 19:24
source