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.