Form::select Orange

Asked

Viewed 370 times

1

I’m having trouble making one Form:select searching information from the database.

How is the Controller;

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

How’s the 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>

1 answer

1


First, the die has a traffic of Controller for View and vice versa, but never the View generate this information directly, example of modification of your 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'));
}

In his 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 only has the variable with the information for your generation and not logic that stays in your Controller.

  • I executed this routine and gave the following error: public Function create() { $setors = Sector::all()->lists('Description', 'id'); $setors[''] = 'Select...'; Return View('internal.create', ['setors' => $setors]); } ;"Class 'App Http Controllers RH Sector' not found"

  • @Aguinaldotupy you need to add the Sector! namespace to your controller

  • Friend, I still have a problem =/ "Method lists does not exist."

  • I made a change in my question has to be like this $setors = Setor::pluck('descricao', 'id') @Aguinaldotupy

  • Shoooow Eternally grateful for the help! The method Return View('internal.create', Compact('$setors')); will not... But it worked with the other method. !

  • If it is useful to vote for it to answer your question, will yes it was written wrong I made a new edition @Aguinaldotupy

Show 1 more comment

Browser other questions tagged

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