How can I include this menu in all my opinions? [Laravel 5]
Good afternoon, I am trying to create a menu from my table, then I want to share it with my view.
Here is what I have tried 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'm doing it
class HomeController extends Maincontroller{}
and then this is my look
@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 mine public_template.header
I just include public_template.sidebar
here the contentpublic_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 am getting this error
Undefined variable: items (View: D: \ XAMPP \ HTDOCS \ sitestarter \ resources \ view \ public_template \ sidebar.blade.php) (View: D: \ XAMPP \ HTDOCS \ sitestarter \ resources \ view \ public_template \ sidebar.blade. php) (View: D: \ XAMPP \ HTDOCS \ sitestarter \ resources \ view \ public_template \ sidebar.blade.php)
how can i fix this? thanks in advance and sorry for my bad english
ref How to pass data to all views in Laravel 5?
I am following the Dhawal Chada method. But I still can't figure it out.
<?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 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 add this line
App\Providers\ComposerMenuProvider::class,
any solution? I still get the same error;
source to share
I am using View Composer for this as follows
I created a new ContactComposer file in the 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 the AppServiceProviders load method I bind all my views to the above View Composer
// Using class based composers...
View::composer(
'*', 'App\Http\ViewComposers\ContactComposer'
);
hope this helps
source to share