Popular select with database values

2

I need a popular select on form with values from another table. Type:

<select>
   <option value="29">Frentista</option>
</select>

I wrote my select like this:

{{ Form::select('set_id_fk', $setores, null, ['class'=>'form-control input-lg']) }}

But in the view it looks like this:

{"set_id": 29, "set_nome": "Frentista"}

I'm using "laravelcollective/html": "^5.3.0" in my project.

Can anyone help me?

    
asked by anonymous 22.11.2016 / 20:16

2 answers

1

If you have a table with unique types and identifiers, it's easy, assuming I have the following table:

id descricao
1   Álcool
2   Gasolina
3   Disel

and from this has my model created, supposing the name of the model FuelType, to assemble the array of items just use the pluck method as follows:

$arrayTipoCombustivel = App\Models\TipoCombustivel::pluck('descricao', 'id');

In this, I would have the $ arrayTypeCombustible this way

[
 '1' => 'Álcool',
 '2' => 'Gasolina',
 '3' => 'Disel'
]

I hope I have helped!

    
26.01.2018 / 17:56
1

When you retrieve the data, try turning it into a list as follows:

$associados = Associado::all()->lists('associado','id');

The list transforms the result into an array with KEY and VALUE, the first parameter is what you want to appear in SELECT, and the key will be the value within the select.

How it works:

<option value="id">associado</option>
    
22.11.2016 / 20:57