Monday, April 3, 2017

Using blade to view the navbar across all the the pages Laravel 5

So,I have a the navbar in my home page where all the module name gets listed from the database. Instead of re-writing the whole code to list module name in all page I decided to use blade's @yeild() functionality.

This is my home page Controller where I get all the module names and pass it to the homepage view like so

public function index()
    {
        $data = Module::get();

        return view("BaseView.home")->with('data',$data);
    }

This is my homepage view where I display all the modules and it's respective name in the navbar like so

<ul class="navbar-nav">
        @foreach($data as $modules)
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="plusMinus" aria-hidden="true"></i>
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                @foreach($modules->module_categories as $category)
                   <?php
                        $getModuleNameToLowerCase = strtolower($modules->module_name);
                        $getCategoryNameToLowerCase = preg_replace('/\s+/', '_', strtolower($category->categories_name));
                    ?>
                    <a class="dropdown-item" href="#"></a>
                @endforeach
            </div>
        </li>
        @endforeach
    </ul>

<div class="container-fluid">
    @yield('mainBody')
</div>

Now When I extend this in another view like so

@extends('BaseView.home')

I get Undefined variable: data.

Can someone help me/guide me on how to solve this issue?



via this.Believer

Advertisement