The technique created to do this is with Viewcomposers which are data that can be shared in several Views of its one-place system. The Viewcomposers is a way to create only one data return and send that information to all Views which can be configured for some, several or all of your Views.
Google Translation
Display composers are call returns or class methods that are called when an display is rendered. If you have data that you want to link to a view whenever this view is rendered, a visualization composer organizes this logic in a single location.
Example:
Create a folder inside app\Http
by the name of ViewComposers
, and create a class inside with the suggested name SideBarComposer
with the following code:
<?php namespace App\Http\ViewComposers;
use Illuminate\View\View;
class SideBarComposer
{
public function __construct()
{
}
public function compose(View $view)
{
$contas_cadastradas = 1;
$jogadores_online = 1;
$view->with('contas_cadastradas', $contas_cadastradas);
$view->with('jogadores_online', $jogadores_online);
}
}
where the values 1 in each variable would be the return of its database for example, can inject by the constructor the models
no problems or use any information recovery technique.
Abra AppServiceProvider
which is located in the folder app\Providers
and within the method boot
add the following line:
public function boot()
{
ViewComposer::composer('*', \App\Providers\SideBarComposer::class);
}
Observing: don’t forget to add the namespace
use Illuminate\Support\Facades\View;
to access the method ViewComposer::composer
.
this configures the created service to be loaded, and in the settings was placed a *
which means that SideBarComposer
will rise every time the information that is shared on all pages.
Note: There is no reason why this information should go up to certain pages array
with the names of their Views
, but from what I read in the question are for all pages.
Finishing now just configure in your view
the following code:
<div class='card'>
<div class="card-header">
Informações Gerais
</div>
<div class="card-body">
<strong>Contas cadastradas: </strong> {{$contas_cadastradas}}<br>
<strong>Player online: </strong> {{$jogadores_online}} <br>
</div>
</div>
The complete example is on the Laravel Framework website, follow the corresponding version of your Laravel
.
Because it doesn’t put in Main template?
– LipESprY
It’ll be the same. Anyway, I’ll have to keep repeating the same code in all methods of all controllers that return a view.
– Davi Alves Bezerra