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

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Did work, still getting the same error
Move the return statement to just above the final bracket.
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;
}

}

2 Comments

still showing undefined variable $data
maybe trying adding an else in the if statement an see what happen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.