How can I concatenate two fields for a drop-down list in Laravel 5.5

1

I have a drop-down list, but it shows me only the value of a column, I have a table called services, the first field is called name and the second is called version, Example

Nombre  
Pgsql    

Version  
9.1

I show the drop-down list with the following code.

{!! Form::mySelect('id_servicio', 
                   'Servicio', 
                    App\Servicios::pluck('version', 'id')->toArray(),       
                    null, 
                   ['class'=>'chosen']) !!}

Due to this I have had to write in the field version, the value also of the type so that the users can see it, Somebody could indicate me how it could concatenate the fields name and fields version please.

    
asked by Omar Noa 15.08.2018 в 16:31
source

1 answer

2

To assemble the text of each element of the list using more than one field of your table, in your query you can concatenate the fields you need and give an alias to the result and refer to the alias you assigned to it.

If your database engine is MySQL, with the function CONCAT() you can join the fields nombre and version , even with a separator between them, for example:

CONCAT(nombre, '-', version)

Taking reference to your code to build the list, the change would be as follows:

{!! Form::mySelect('id_servicio', 
               'Servicio', 
                App\Servicios::select(DB::raw("CONCAT(nombre, '-', version) AS nombre_version"), "id")->pluck('nombre_version', 'id')->toArray(),       
                null, 
               ['class'=>'chosen']) !!}

With the previous code, the result would be an arrangement like the following:

[
    1 => "Pgsql - 9.1",
    ...
]

I hope it helps.

Greetings.

    
answered by 15.08.2018 / 17:18
source