Laravel - How popular a combobox in the 'on load' event of a view (Blade)

Asked

Viewed 2,873 times

2

I need to popular a combobox with database data, but this fill should occur while loading the view.

Since the view is not the right place to write code, how to feed the combobox? Through a method in a controller? What 'mechanism' to respond to the 'on load' event for example?

As a demonstration, you would have the form below in a view:

<form>
 Cliente: 
<Select>
    <option>Fulano de Tal</option>
</select>
</form>

2 answers

1

To feed a combobox by Laravel 5 recommend you install by Composer the package Laravel Collective to create forms by Form facade and facilitate working with forms. If the combobox needs to be powered by values queried in the database, you should query the controller using the model within the method that loads the view.

Example:

Imagine you have a state mobile and want to feed the combobox

$estado = Estado::lists('uf','id')->all();

moving to the view

return view('sua view',compact('estado'));

or

return view('sua view',get_defined_vars());

Creating the combobox in the view and displaying the data with the Collective Laravel:

{!! Form::select('estados',['' => 'selecione ...'+$estado],'',['id' => 'estados']) !!}

One of the advantages of creating forms using the Collective Standard is for editing information. When you edit a user’s data for example, you have to query and fill in all the form fields with their respective data.

For example:

To fill in all the fields of a user form you query the method, send the object to the view and ...

{!! Form::model($seuObjetoUsuario) !!}

{!! Form::text('nome',app('request')->get('nome')) !!}

.... These lines show how you fill in a form that has the name field using the object.

0

How to get the specific value gives key Field, but list the entire list if the person wants to change the state?
In Cakephp user a list like this:

$cidade= $this->cidade->estado->find('list',['keyField' => 'idcidade', 'valueField' => 'nome_estado','limit' => 20]);

In the Laravel as I do?

Browser other questions tagged

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