Viktor Koncsek
Viktor Koncsek

Reputation: 574

Include more controllers into a single page

I just started learning Laravel, and I want to include a second controller into my main layout.

The route is the default root directory: /

And the layout looks like this:

<div class="container">
    @yield('content')
</div>
<div class="basket">
    ~basket comes here~
</div>

I want to show the user's basket, but I need DB queries for that, and I can't find a way to include an other controller, atleast not with routing.

I'm not really asking for code (Sadly i didn't find a better place for this question), probably I just need a designing tip, I really feel I'm trying to do it wrong, since I couldn't find any relevant/helpful information for this.

And I dont want to put the basket into every controller that uses my main layout.

Any kind of help would be apreciated, I'm really lost :)

Upvotes: 0

Views: 44

Answers (1)

thefallen
thefallen

Reputation: 9749

You should use view composers. Open you AppServiceProvider and inside the boot() method add the following:

view()->composer('your.layout.name', function ($view) {
    $basket = ...// Your basket query here

    $view->with('basket', basket);
});

This basically says, when view with name your.layout.name is composed, add variable with name $basket.

Upvotes: 1

Related Questions