9 months ago

Passing the same variables to multiple views over and over again

Posted 9 months ago by Shoken

Hey everyone, I've recently started using Laravel for a university project. It is AMAZING.

I have one issue I haven't been able to solve myself, though. Basically I have a main nav menu stored in the file main.blade.php.

This file is extended by all the other views, including "index.blade.php" and "about.index.php".

Now, when returing index through my FrontEndController, I just prepare an array of variables and return it with the view in order to dinamically generate my menus and categories. Basically my function looks like this:


public function index() {

    $menu = MainMenu::OrderBy('Order', 'asc')->get();
    $cat = Category::OrderBy('sortOrder', 'asc')->get();

    $data = array(
        'main_menus' => $menu,
        'categories' => $cat,

    return view('front_end.index')->with($data);



Note that all these variables are used by the menu, not the content. Now I've started working on "about.index.php" and I noticed that I have to send the same $data array all over again in order to prepare the menu once more. So I basically need to send the same array over and over again for all the views.

This seems redundant and repetitive, and I feel like I am missing something. Is there a better way to do this?

Please sign in or create an account to participate in this conversation.