我得到的$navs
没有在我的Blade视图中定义,尽管我已经定义了它并由我的控制器传递到视图。不管怎样,我仍然会出现以下错误。
ErrorException未定义变量:navs(视图:C: \Users\FreecodingBoy\Downloads\NewWorld新文件夹\resources\views\layout\NewWorld\navs\logo.blade.php(
刀片/视图
@foreach($navs as $n)
<li class="u-has-submenu u-header-collapse__submenu">
<a class="u-header-collapse__nav-link u-header-collapse__nav-pointer" href="javascript:;"
data-target="#headerSidebarComputersCollapse" role="button" data-toggle="collapse" aria-expanded="false"
{{ $n['category']->Companyname }}
</a>
<div id="headerSidebarComputersCollapse" class="collapse" data-parent="#headerSidebarContent">
<ul class="u-header-collapse__nav-list">
<li><span class="u-header-sidebar__sub-menu-title"></span></li>
@foreach($n['subcategory'] as $sub)
<li class=""><a class="u-header-collapse__submenu-nav-link" href="">{{ $sub->brandname }}</a></li>
@endforeach
控制器
public function index()
{
$nav = array();
$cate = Category::all();
foreach ($cate as $c) {
$v = array();
$subcate = Subcategory::where('catergoriesid', '=', $c->id)->get();
foreach ($subcate as $s) {
array_push($v, $s);
}
array_push($nav, [
'category' => $c, 'subcategory' => $v
]);
}
$product = Products::join('subcategory', 'products.subcategoriesid', '=', 'subcategory.id')
->join('categories', 'products.categoriesid', '=', 'categories.id')
->select('products.*', 'subcategory.brandname', 'categories.Companyname')
->get();
$carousel = carousel::all();
$cart = cart::join('products', 'cart.productid', '=', 'products.id')
->join('users', 'cart.userid', '=', 'users.id')
->select('cart.*', 'products.productName', 'products.price', 'products.photo')
->get();
$total = 0;
if ($cart != null) {
foreach ($cart as $c) {
$total = ($c->price * $c->quantity);
}
return view('NewWorld/index')->with('products', $product, 'navs',
$nav, 'ca', $carousel, 'cart', $cart, 'total', $total);
}
}
您没有正确地从控制器返回数据。您应该返回的是一个具有键/值对的数组。请尝试以下操作。
...
return view('NewWorld/index')
->with(['products' => $product, 'navs' => $nav, 'ca' => $carousel,
'cart' => $cart, 'total' => $total]);
}
此外,请确保关闭Blade/view文件中的<a
标记。
<a class="u-header-collapse__nav-link u-header-collapse__nav-pointer"
href="javascript:;" data-target="#headerSidebarComputersCollapse"
role="button" data-toggle="collapse" aria-expanded="false">
{{ $n['category']->Companyname }}
</a>
您可以在控制器中尝试此操作以传递数据。
public function index() {
$nav = array();
$cate = Category::all();
foreach ($cate as $c) {
$v = array();
$subcate = Subcategory::where('catergoriesid', '=', $c->id)->get();
foreach ($subcate as $s) {
array_push($v, $s);
}
array_push($nav, [
'category' => $c, 'subcategory' => $v
]);
}
$product = Products::join('subcategory', 'products.subcategoriesid', '=', 'subcategory.id')
->join('categories', 'products.categoriesid', '=', 'categories.id')
->select('products.*', 'subcategory.brandname', 'categories.Companyname')
->get();
$carousel = carousel::all();
$cart = cart::join('products', 'cart.productid', '=', 'products.id')
->join('users', 'cart.userid', '=', 'users.id')
->select('cart.*', 'products.productName', 'products.price', 'products.photo')
->get();
$total = 0;
if ($cart != null) {
foreach ($cart as $c) {
$total = ($c->price * $c->quantity);
}
return view('NewWorld/index')
->with(['products' => $product, 'navs' => $nav, 'ca' => $carousel,
'cart' => $cart, 'total' => $total]);
} else {
return view('NewWorld/index')
->with(['products' => $product, 'navs' => $nav, 'ca' => $carousel;
}
}