Laravel - Creating select with Form :: select + Model :: lists

2

I'm trying to create a select with the following code:

My controller looks like this:

public function create(){
    $marcas = Marca::lists('descricao', 'id')->toArray();
    return view('sistema.modelos.create', ['marcas'=>$marcas]);
}

My view looks like this:

{!! Form::open(['route'=>'sistema.modelos.store']) !!}
{!! Form::select('marca_id', $marcas ) !!}
{!! Form::close() !!}

You are giving the following error:

Trying to get property of non-objec

Has anyone ever gone through this? Do you know how to do this?

editing ---

I've already done this:

$marcas = Marca::all(['id', 'descricao']);
return view('sistema.modelos.create', compact('marcas',$marcas));

According to the link: link

In this case the select up is rendered, but looks like this:

<select name="marca_id"><option value="0">{id : 1 , descricao : NOME}</option></select>
    
asked by anonymous 20.07.2016 / 03:09

1 answer

2

You should initialize select with an array and not a collection, try changing

$marcas = Marca::lists('descricao', 'id');

for

$marcas = Marca::lists('descricao', 'id')->toArray();

or in Laravel 5.2

$marcas = Marca::pluck('descricao', 'id');

If you want a neutral value for select, try to use the placeholder attribute or give append before transforming the collection into an array.

    
20.07.2016 / 03:17