我创建了一个带有动态菜单的菜单栏。当我将其单独加载为刀片模板页面时,它工作正常。 但是当我将其包含在母版页布局中时显示错误。 路线文件::
Route::resource('menu',MenuController::class);
Route::resource('submenu',SubMenuController::class);
Route::view('master','layouts.layout');
我的菜单控制器::
public function index()
{
$menus = menu::all();
return view('layouts.include-menu', compact('menus'));
}
layouts.include-menu.blade.php 页面 // 删除了所有其他详细信息
<header id="header" class="header-two">
<div class="site-navigation">
<div class="container">
<div class="row">
<div class="col-lg-12">
<nav class="navbar navbar-expand-lg navbar-light p-0">
<div class="logo">
<a class="d-block" href="index-2.html">
<img loading="lazy" src="images/logo.png" alt="Constra">
</a>
</div><!-- logo end -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbar-collapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbar-collapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav ml-auto align-items-center">
@foreach ($menus as $item)
@if (count($item->submenus)>0)
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">{{ $item->menu_name }}<span class="ion-ios-arrow-down"></span></a>
@foreach ($item->submenus as $submenu)
<ul class="dropdown-menu" role="menu">
<li><a href="index.html">{{$submenu->submenu_name}}</a></li>
</ul>
@endforeach
</li>
@else
<li class="nav-item">
<a class="nav-link" href="contact.html">{{ $item->menu_name }}</a>
</li>
@endif
@endforeach
</ul>
</div>
</nav>
</div>
<!--/ Col end -->
</div>
<!--/ Row end -->
</div>
<!--/ Container end -->
</div>
<!--/ Navigation end -->
</header>
<!--/ Header end -->
当我将其作为刀片模板页面单独加载时,此页面加载正常。
但是当我将其包含在母版页中时出现错误::
<!doctype html>
<html>
<head>
@include('layouts.include-head')
</head>
<body>
<!--/ main div of the page -->
<div class="body-inner">
<!-- Header start -->
<header id="header" class="header-two">
@include('layouts.include-menu')
</header> <!-- Header end -->
<footer id="footer" class="footer bg-overlay">
@include('layouts.include-footer')
</footer>
</div> <!--/ main div of the page ends -->
</body>
</html>
C:\xampp\htdocs\gva_app 上显示错误 资源 iews\layouts\include-menu.blade.php ::
Undefined variable $menus
@foreach ($menus as $item)
为什么会这样?
您现在可以包含 include-menu 并传递 $menus 变量
<header id="header" class="header-two">
@include('layouts.include-menu', ['menus' => $menus])
</header>