3

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;

YVS1102
  • 2,658
  • 5
  • 34
  • 63

1 Answers1

3

I am using View Composer for this as follows

I have created a new file called ContactComposer in Http/ViewComposers folder like this

namespace App\Http\ViewComposers;

use Illuminate\View\View;
//use App\Repositories\UserRepository;

class ContactComposer
{
    /**
    //  * The user repository implementation.
    //  *
    //  * @var UserRepository
    //  */
    // protected $users;

    // *
    //  * Create a new profile composer.
    //  *
    //  * @param  UserRepository  $users
    //  * @return void

    // public function __construct(UserRepository $users)
    // {
    //     // Dependencies automatically resolved by service container...
    //     $this->users = $users;
    // }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('PhoneNumber', '1111111111');
    }
}

and inside AppServiceProviders Boot method I am binding all my views to the above View Composer

// Using class based composers...
        View::composer(
            '*', 'App\Http\ViewComposers\ContactComposer'
        );

hope this helps

Dhaval Chheda
  • 4,637
  • 5
  • 24
  • 44