How to fill a select with Eloquent?

Asked

Viewed 3,707 times

0

I’m using the Laravel 5.5 and trying to fill a input select with the Eloquent and using the following code:

In the controller:

$tipos = Tipo::pluck('nome', 'id');
return view('teste')->with('tipos', $tipos);

In the view:

{{Form::select('tipo_id',
               $tipos,
               $imovel->tipo_id,
               ['id'=>'myselect','class' =>'form-control'])}}

and in this way, each item of the checkbox turns an object as follows:

{"id":1,"nome":"casa","created_at":"2017-12-29 18:09:45","updated_at":"2017-12-29 18:09:45"}

How do I keep only the attribute nome in the item and id in the value?

  • It seems to me that you’re correct I don’t understand the problem!

  • In the documentation of the Laravel Pluck is used in a slightly different way. There it is additionally called return view('teste')->with('tipos', $tipos->all()); that should return an associative array, in the format ['chave1'=>valor1, 'chave2'=>valor2]

  • @Virgilionovic the problem is that each checkbox item is coming as an object, but I want a key-value array in which the value is the name and the key the id. It makes no sense to show the user the whole object

  • @Juven_v even using $tipos->all(), I still have the same result, do you have any other idea? to kind of desperate with this

  • You can mount a loop for and create the desired array. Example: $arraytipos = []; foreach($tipos->all() as $tipo){$decode = json_decode($tipo); $arraytipos[$decode['id']] = $decode['nome']} . Then just do $tipos = $arraytipos;. Not the best approach, maybe you can take a look at value

  • Form::select return one select? I don’t understand why the code still seems correct

  • I read, reread, but, the Form::select return a checkbox and not a checkbox and the pluck Eloquent method, really does what you need, I still don’t understand your problem ? has how to exemplify the problem in a minimal example.

Show 2 more comments

2 answers

0

It’s very simple!

In the controller should stay this way:

Just reverse the order of the factors and get a different result. No need to scroll through the object!

$tipos = Tipo::pluck('id', 'nome');
return view('teste')->with('tipos', $tipos);

In the View - Blade

{{Form::select('tipo_id',
           $tipos,
           $imovel->tipo_id,
           ['id'=>'myselect','class' =>'form-control'])}}

0

You will have to treat the array passed to select to form a new associative array. The key being id and the name the value.

$tipos = Tipo::pluck('nome', 'id');
$tipo_associativo = [];
foreach($tipos as $key => $tipo){

        $tipo_associativo[$tipo['id']] = $tipo["nome"]; 
}
return view('teste')->with('tipos', $tipo_associativo);

now when you pass this new array to Form::select:

{{Form::select('tipo_id',
           $tipos,
           $imovel->tipo_id,
           ['id'=>'myselect','class' =>'form-control'])}}

select will be mounted as follows:

<select name="tipo_id">
    <option value="id">nome</option>

</select>

In the official documentation It’s a little short, but it gives find other sources which explains well how to create a select based on array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.