3

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.

1

2 Answers 2

4

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'));
    });
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can share a variable in all of your views from app service provider's boot method.

public function boot()
{

    $mainMenu = $this->retornaNavegacao(),

     view()->share(compact('mainMenu'));
}

If you want to share the variable in just some specific views you can make your own view composer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.