Form :: select Laravel

1

I'm having a hard time making a Form:select that fetches information from the database.

How is Controller ;

public function create() 
{   
    return View('internos.create');
}

As it is in View ;

<div class="form-group">
  {{ Form::label('setor', 'Setor: ') }}
  {{ Form::select('setor_id', 
       array(''=>'Selecione...')+ Setor::all()->lists('descricao', 'id'), 
       null, array('class'=>'form-control'))  
  }}
</div>
    
asked by anonymous 17.12.2018 / 18:54

1 answer

1

First, the data has a traffic from Controller to View and vice versa, but never View generate this information directly, example of modifying its Controller method :

public function create() 
{   
    $setors = Setor::pluck('descricao', 'id')
    $setors[''] = 'Selecione...';
    return View('internos.create', ['setors' => $setors]);
    // ou return View('internos.create', compact('setors'));
}

On your View make the following modifications:

<div class="form-group">
  {{ Form::label('setor', 'Setor: ') }}
  {{ Form::select('setor_id', $setors, null, array('class'=>'form-control')) }}
</div>

That is, in its View it only has the variable with the information for its generation and not logic that stays in its Controller .

    
17.12.2018 / 19:11