I'm making a website where the menu is manageable by the administrator, my question is how to mount that select to display on all pages.
At first I was doing the query of the menus in all actions of the controller, but I would like to optimize this, just do not know how.
My Controller.
<?php
namespace App\Http\Controllers;
use App\Categorias;
use Illuminate\Http\Request;
class FrontendController extends Controller {
public $template = 'default';
// Retorna Navegação
public function retornaNavegacao(){
$sql = Categorias::where([
['exibir', '=', 1], ['publicado', '=', 1]
])
->get();
return $sql;
}
// Página 'Categorias'
public function categoria(){
return view('frontend.'.$this->template.'.categorias.index',
array(
'mainMenu' => $this->retornaNavegacao(),
)
);
}
// Página 'Produtos'
public function produto(){
return view('frontend.'.$this->template.'.produtos.index',
array(
'mainMenu' => $this->retornaNavegacao(),
)
);
}
// Página 'Contato'
public function contato(){
return view('frontend.'.$this->template.'.contato.index',
array(
'mainMenu' => $this->retornaNavegacao(),
)
);
}
}
The function retornaNavegacao() only queries the database and returns all registered menus (There is a relationship to search for all subcategories).
However I have to repeat the menu code in all the actions of the controller, I think it might have a smarter way of not having to repeat this code in all actions.
In AppServiceProvider
class edit boot
method
public function boot()
{
$categories = Categorias::where([
['exibir', '=', 1],
['publicado', '=', 1]
])
->get();
View::composer('*', function ($view) use ($categories) {
$view->with(['mainMenu' => $categories]);
});
}
Also you can use all power of laravel
public function boot()
{
$mainMenu = Categorias::whereWxibir(1)
->wherePublicado(1)
->get();
View::composer('*', function ($view) use ($mainMenu) {
$view->with(compact('mainMenu'));
});
}