0

I'm getting $navs is not defined in my Blade view, even though I have it defined and passed to the view by my controller. I am still getting the following error regardless.

ErrorException Undefined variable: navs (View: C:\Users\FreecodingBoy\Downloads\NewWorld-newfolder\resources\views\layout\NewWorld\navs\logo.blade.php)

Blade/View

@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

Controller

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);
    }
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95

2 Answers2

0

You are not returning the data from your controller correctly. What you should be returning is an array with key/value pairs. Try the following.

...
    return view('NewWorld/index')
        ->with(['products' => $product, 'navs' => $nav, 'ca' => $carousel, 
            'cart' => $cart, 'total' => $total]);
}

Also, make sure you close your <a tag in the Blade/view file.

<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>
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
0

You can try this in your controller to past the data.

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

}

MamaWin
  • 330
  • 1
  • 2
  • 11