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!
– novic
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]
– Juven_v
@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
– Gabriel Augusto
@Juven_v even using $tipos->all(), I still have the same result, do you have any other idea? to kind of desperate with this
– Gabriel Augusto
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– Juven_v
Form::select return one
select
? I don’t understand why the code still seems correct– novic
I read, reread, but, the
Form::select
return a checkbox and not acheckbox
and thepluck
Eloquent method, really does what you need, I still don’t understand your problem ? has how to exemplify the problem in a minimal example.– novic