Laravel - How to use a combobox in the 'on load' event of a view (blade)

2

I need a combobox with data coming from the database, but this fill should occur when loading the view.

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

As a demonstration, I would have the following form on a view blade:

<form>
 Cliente: 
<Select>
    <option>Fulano de Tal</option>
</select>
</form>
    
asked by anonymous 17.05.2016 / 13:33

3 answers

2

To feed a combobox through Laravel 5, I recommend that you install the Laravel collective package to create forms by the Form facade and facilitate the work with forms. If the combobox needs to be fed by values consulted in the database, you should query the controller using the model within the method that loads the view.

Example:

Imagine that you have a statesmodel 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 laravel collective:

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

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

For example:

To fill all the fields of a user form you query in 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 a form that has the name field using the object.

    
17.05.2016 / 19:36
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 Laravel how do I do it?

    
27.10.2017 / 22:43
-1

Looking a lot on the web, I learned that the way to load a variable into a view (blade) when it is called is through the Service Provider feature.

In general terms, you inform Laravel that the view of your choice will be registered in a class of your own in order to do any action n the moment it is loaded

.

>

In general terms, I saw that my project, at the moment of the call to the view in question, goes through the app \ Providers directory in search of existing files there. Checking each one of them, it searches in its interior by the name of my chosen view, in a line as below:

view () -> composer ('', '' ');

The above command means: 'If they call the view ViewName , it goes to the objectname \ Http \ ViewComposers directory and opens the ProfileComposer.php >. The ProfileComposer class that exists within this file has a compose name method. This compose () method has a $ view parameter, which is the name of the view that caused the call. In the body of this method will have any arrangement to feed the variable that will be passed to the view in question.

The method would look like this:

public function compose(View $view)
    {
        $clientes =DB::select('select * from minhatabela');
        $view->with('clientes',$clientes);
    }

Within the view being passed by the Provider service, we could simply do

<select>
 @foreach($clientes as $c)
    <option>{{$c->cliente}}</option>
 @endforeach
</select>

That's it.

However, the general guidelines above are not enough, because a file called app.php was still missing.

I solved my problem by doing the following steps:

1 - At the command prompt where my project laravel is installed, I have commanded the following:

php artisan make:provider ComposerServiceProvider

The name 'ComposerServiceProvider' is a php file that appears in the app \ Providers folder. It may be any name, but this name will refer to later.

2 - Open the app.php file that is in the config folder (this folder is subordinate to the project name). In the

'providers' => [

and below the commented block

/*
* Application Service Providers...
*/

I've included the reference

...
nomeDoMeuProjeto\Providers\ComposerServiceProvider::class,
...

3 - I created a ViewComposers folder hanging in the Http folder:

4 - Within this folder, I created a php file named ProfileComposer.php with the following content:

<?php 
namespace nomeDoMeuProjeto\Http\ViewComposers;

use Illuminate\Contracts\View\View;
use Tempo\ClientesModel; //nome do meu Model
use Illuminate\Support\Facades\DB;//isto porque estou usando DB::select

class ProfileComposer {
    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $clientes = DB::select('select * from nomedaMinhaTabela');
        $view->with('clientes',$clientes);
    }

}
?>

5 - I opened my view in question and simply put the code below in a strategic point:

<select>
    @foreach($clientes as $c)
        <option>{{$c->cliente}}</option>
    @endforeach
</select>

I would like to point out strongly that this was a solution of mine, not something that can be recommended as a best practice.

    
17.05.2016 / 16:25