How can i include this menu to all my view ? [Laravel 5]

Viewed 1479

Good day, I'm trying to create a menu from my table then i want to share it to my view.

Here is what i try so far

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Menu as menumodel;
class Maincontroller extends Controller
{
    //
    public function __construct()
     {
        $items = menumodel::tree();
        View::make('items', $items);
    }
}

then in my controller (HomeController). I do this

class HomeController extends Maincontroller{}

and then this is my view

@include('public_template.header')
<div class="right_col" role="main">
  <div class="">
    <div class="page-title">
      <div class="title_left">
        <h3>POST <small> Page to demonstrate multilevel menu</small></h3>
      </div>
    </div>
  </div>
</div>
@include('public_template.footer')

In my public_template.header i simply include public_template.sidebar here is the content of public_template.sidebar

 <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
              <div class="menu_section">
                <h3>General</h3>
                 <ul class="nav side-menu">
                  @foreach($items as $item)
                  <li><a><i class="fa fa-home"></i> Home <span class="fa fa-chevron-down"></span></a>
                    <ul class="nav child_menu">
                      <li><a href="index.html">Dashboard</a></li>
                      <li><a href="index2.html">Dashboard2</a></li>
                      <li><a href="index3.html">Dashboard3</a></li>
                    </ul>
                  </li>
                  @endforeach
                </ul>
              </div>

            </div>

But with my script above i get this error

Undefined variable: items (View: D:\xampp\htdocs\sitestarter\resources\views\public_template\sidebar.blade.php) (View: D:\xampp\htdocs\sitestarter\resources\views\public_template\sidebar.blade.php) (View: D:\xampp\htdocs\sitestarter\resources\views\public_template\sidebar.blade.php)

how can i fix it ? thanks in advance and sorry for my bad english

ref How to pass data to all views in Laravel 5?


I'm following Dhaval Chheda method. But i still can't get that.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ComposerMenuProvider extends ServiceProvider
{

    public function boot()
    {
        view()->composer(
            'app',
            'App\Http\ViewComposers\MenuComposer'
        );    }


    public function register()
    {
        //
    }
}

Then i create MenuComposer

<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;

    class MenuComposer
    {
        public $menu = [];
        /**
         * 
         *
         * @return void
         */
        public function __construct()
        {

            $this->items = menumodel::tree();

        }

        public function compose(View $view)
        {
            $view->with('items', end($this->items));
        }
    }

and then in app\config.php i add this line

    App\Providers\ComposerMenuProvider::class,

any solution ? i still get same error;

1 Answers
Related