php – Popular select with values ​​from the database

Question:

I need to populate a select on the 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 the view is getting like this:

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

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

Can someone help me?

Answer:

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, have my model created, assuming 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 $arrayTypeCombustivel that way

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

Hope this helps!

Scroll to Top