Shoken's avatar

Passing the same variables to multiple views over and over again

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?

0 likes
8 replies
Shoken's avatar

@TOMOPONGRAC - That seems exactly what I need, but I am a bit confused on the values of 'key' and 'value'. Now I wrote, in the class AppServiceProvider:

''' public function boot() {

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

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

    View::share('data', $data);
}

'''

But this does not seem to work, as categories is an undefined variable.

tomopongrac's avatar

What you get with this

public function boot() {

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

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

dd($data);

    View::share('data', $data);
}
Shoken's avatar

@TOMOPONGRAC - I get a very long dump of the data in the DB. Not sure if I should post it all here, but here's a fraction of it for reference:

array:2 [ "main_menus" => Illuminate\Database\Eloquent\Collection {#170 #items: array:3 [ 0 => App\MainMenu {#171 #connection: "mysql" #table: "main_menus" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:6 [ "id" => 1 "created_at" => null "updated_at" => null "name" => "Homepage" "URL" => "/" "Order" => 1 ] #original: array:6 [ "id" => 1 "created_at" => null "updated_at" => null "name" => "Homepage" "URL" => "/" "Order" => 1 ]

.... etc...

Snapey's avatar

This

View::share('data', $data);

}

Creates a variable $data. Categories will be within that

Alternatively just view share each variable rather than putting them in an array

Shoken's avatar

@tomopongrac it works now! I made a mistake when calling for those variables.

Before, I returned the views with the method with(). That allowed me to call the variables directly with $categories and such.

Now I have to do $data['categories'] so it can work. My understanding is this has to do with the fact that I'm working with an array and no longer an object, but I might be wrong.

Anyway, in case anyone else needs this, I'm going to leave this here. :) Thanks for the help.

Snapey's avatar

just

public function boot() {

    View::share('menu', MainMenu::OrderBy('Order', 'asc')->get());

    View::share('categories', Category::OrderBy('sortOrder', 'asc')->get());

}
1 like

Please or to participate in this conversation.