Laravel 中的 Blade/view 中未定义变量

问题描述 投票:0回答:2

我得到

$navs
未在我的 Blade 视图中定义,即使我已定义它并由我的控制器传递到视图。不管怎样,我仍然收到以下错误。

ErrorException 未定义变量:navs(查看: C:\Users\FreecodingBoy\Downloads\NewWorld-newfolder 资源 iews\layout\NewWorld avs\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);
    }
}
php laravel laravel-8 laravel-controller
2个回答
0
投票

您没有正确从控制器返回数据。您应该返回的是一个带有键/值对的数组。尝试以下方法。

...
    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>

0
投票

您可以在控制器中尝试此操作来传递数据。

    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;
}

}

© www.soinside.com 2019 - 2024. All rights reserved.